在WordPress中检索带有pre_get_posts的文章时,如何将ACF重复字段中的数据添加到条件中


例如,在重复字段(事件)的自定义字段中设置开始日期(开始)和结束日期(完成)
如果要在当前日期介于两者之间时获取文章,请

query.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function change_posts_per_page($query) {
    //管理画面のメインクエリーとメインクエリーじゃないときは処理しない
    if (is_admin() || !$query->is_main_query()) return;

    // 特定のクエリにて処理
    if ($query->is_category() || $query->is_tag() || $query->is_search()) {
        // 20件取得
        $query->set('posts_per_page', 20);
        // 本日日付取得
        $today = date_i18n('Ymd');
        // SQLを変更させるフラグとして設置
        $query->set('wildcard_meta_key',true);
        $query->set(
            'meta_query', array(
                array(
                    'key' => 'event_%_start',
                    'value'   => $today,
                    'compare' => '<='
                ),
                array(
                    'key' => 'event_%_finish',
                    'value'   => $today,
                    'compare' => '>='
                ),
                'relation'=>'AND'
            )
        );

    }


}
add_action( 'pre_get_posts', 'change_posts_per_page' );

// リピータ部分のワイルドカード対応
function my_posts_where_wildcard( $where, $query ) {
    if ( $query->get( 'wildcard_meta_key' ) ) {
        $where = str_replace( 'meta_key =', 'meta_key LIKE', $where );
    }
    return $where;
}
add_filter( 'posts_where', 'my_posts_where_wildcard', 10, 2 );

和functions.php系统中进行描述。
添加多个meta_query处理时,将设置Wildcard_meta_key。
排除那些不针对重复字段的对象。