EventManagerでは、ショートコードを利用することで近日開催されるイベントを5件表示したい、とか今日開催されるイベントを表示したい、などといったイベント表示が可能です。
ショートコードにつかえる値と使い方サンプルは下記ページに掲載されています。
https://wp-events-plugin.com/documentation/event-search-attributes/
その中で、今月のイベントをすべて表示したいが、scope="month"の設定ではすでに終了しているイベントも表示されてしまうことがわかりました。
明日以降でええんや…とフォーラムを探していたところ独自のイベントスコープを作成することで希望通りの表示ができるとのことでしたのでやってみました。
明日~今月末までのイベントを表示するイベントスコープを作成する
理想は見出し通りです。明日から今月末(例:2021年10月の場合、2021年10月31日まで)のイベントを表示するためのイベントスコープを作成します。
独自スコープをセットする
add_filter( 'em_events_build_sql_conditions', 'my_em_scope_conditions',1,2); function my_em_scope_conditions($conditions, $args){ if( !empty($args['scope']) && $args['scope']=='tomorrow-month' ){ $start_date = date('Y-m-d',strtotime("+1 day", current_time('timestamp'))); $end_date = date('Y-m-d',strtotime('last day of this month')); $conditions['scope'] = " (event_start_date BETWEEN CAST('$start_date' AS DATE) AND CAST('$end_date' AS DATE)) OR (event_end_date BETWEEN CAST('$end_date' AS DATE) AND CAST('$start_date' AS DATE))"; } return $conditions; }
「scope」SQL条件を置き換えて、明日から今月末までの日付を検索します。
イベント表示開始日は明日だよ、という設定が下記、
$start_date = date('Y-m-d',strtotime("+1 day", current_time('timestamp')));
イベント表示終了日は月末だよ、という設定が下記、
$end_date = date('Y-m-d',strtotime('last day of this month'));
strtotime()関数の引数を「last day of this month」にすることで今月の月末の日付を取得することが出来るようです。
スコープをショートコードから使用できるようにします
次にセットしたスコープをショートコードから使用できるようにします。
add_filter( 'em_get_scopes','my_em_scopes',1,1); function my_em_scopes($scopes){ $my_scopes = array( 'tomorrow-month' => 'Tomorrow and Month' ); return $scopes + $my_scopes; }
これでショートコードを使用するときにscope="tomorrow-month"とすることで明日~今月末までのイベントが表示できるようになります。
参考:https://wp-events-plugin.com/tutorials/create-your-own-event-scope/