プラグインThe Events Calendarのtec_events_custom_tables_v1_custom_tables_query_pre_get_postsフィルタの使用方法・解説

概要

tec_events_custom_tables_v1_custom_tables_query_pre_get_posts フィルタは、The Events Calendar プラグインにおいて、カスタムテーブルを使用したイベントのクエリを修正するために利用されます。このフックを使用することで、イベントの表示方法や条件をカスタマイズすることが可能になります。具体的には、次のような機能を実装する際によく使用されます。

  1. 特定のイベントカスタムフィールドの条件を基に結果をフィルタリングする。
  2. カスタムの投稿タイプを持つイベントの表示を調整する。
  3. 日付や時間に基づいてイベントをソートする。
  4. ユーザーの権限に応じたイベント表示の制御を行う。
  5. クエリのパラメータを動的に追加・修正する。
  6. デフォルトのクエリを上書きし、特定の条件にマッチするイベントだけを返す。

構文

add_filter('tec_events_custom_tables_v1_custom_tables_query_pre_get_posts', 'custom_function_name', 10, 2);

パラメータ

  • $query : WP_Query オブジェクト。現在処理中のクエリ。
  • $instance : イベントのカスタムテーブルインスタンス。

戻り値

  • 修正された WP_Query オブジェクト。

互換性情報

  • The Events Calendar バージョン: 5.x 以降
  • WordPress バージョン: 4.9 以降

この関数のアクションでの使用可能性

アクション 使用例
mu_plugin_loaded
registered_post_type
plugins_loaded
wp_roles_init
setup_theme
after_setup_theme
set_current_user
init
register_sidebar
wp_loaded
send_headers
parse_query
pre_get_posts
wp
template_redirect
get_header
wp_head

サンプルコード

サンプルコード1: 特定のカスタムフィールドでフィルタリング

add_filter('tec_events_custom_tables_v1_custom_tables_query_pre_get_posts', function($query) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_post_type_archive('tribe_events') ) {
        $meta_query = $query->get('meta_query');

        $meta_query[] = array(
            'key'     => 'event_type',
            'value'   => 'webinar',
            'compare' => 'LIKE',
        );

        $query->set('meta_query', $meta_query);
    }
}, 10, 2);

このサンプルコードは、イベントアーカイブページで「event_type」というカスタムフィールドが「webinar」のイベントのみを表示します。

サンプルコード2: 日付でソートする

add_filter('tec_events_custom_tables_v1_custom_tables_query_pre_get_posts', function($query) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_post_type_archive('tribe_events') ) {
        $query->set('orderby', 'event_date');
        $query->set('order', 'ASC');
    }
}, 10, 2);

このコードは、イベントをイベントの日付で昇順にソートします。

サンプルコード3: ログインユーザーのみ表示

add_filter('tec_events_custom_tables_v1_custom_tables_query_pre_get_posts', function($query) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_post_type_archive('tribe_events') ) {
        if ( is_user_logged_in() ) {
            // ログインユーザーのみのロジック
        } else {
            $query->set('posts_per_page', 0);
        }
    }
}, 10, 2);

このサンプルは、ログインしていないユーザーにはイベントを表示しないという条件を設定します。

サンプルコード4: カスタム条件に基づいたクエリの変更

add_filter('tec_events_custom_tables_v1_custom_tables_query_pre_get_posts', function($query) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_post_type_archive('tribe_events') ) {
        $query->set('meta_query', array(
            'relation' => 'OR',
            array(
                'key'     => 'location',
                'value'   => 'Tokyo',
                'compare' => '='
            ),
            array(
                'key'     => 'event_status',
                'value'   => 'active',
                'compare' => '='
            ),
        ));
    }
}, 10, 2);

このサンプルは、「locationがTokyo」または「event_statusがactive」のイベントのみを表示します。

サンプルコード5: 特定のイベントカテゴリの絞り込み

add_filter('tec_events_custom_tables_v1_custom_tables_query_pre_get_posts', function($query) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_post_type_archive('tribe_events') ) {
        $query->set('tax_query', array(
            array(
                'taxonomy' => 'tribe_events_cat',
                'field'    => 'slug',
                'terms'    => 'webinars',
                'operator' => 'IN',
            ),
        ));
    }
}, 10, 2);

このコードは、「webinars」カテゴリーに属するイベントのみを表示します。

この関数について質問する


上の計算式の答えを入力してください