与当前日期比较时,无法通过多个元键查询帖子

时间:2019-07-08 17:05:51

标签: php mysql wordpress advanced-custom-fields acfpro

我正在编写代码以根据其他元值比较日期查询帖子元值,以下是我的代码,它查询所有忽略日期条件的帖子:

我尝试使用'LEFT JOIN'语句失败

$querystr = "
SELECT DISTINCT m2.meta_value as 'appdata'
FROM $wpdb->posts
INNER JOIN $wpdb->postmeta m1 ON ($wpdb->posts.ID = m1.post_id AND m1.meta_key LIKE 'repeater_%_date' AND m1.meta_value > NOW())
INNER JOIN $wpdb->postmeta m2 ON ($wpdb->posts.ID = m2.post_id AND m2.meta_key LIKE 'repeater_%_appdata' AND m2.meta_value != '')
WHERE $wpdb->posts.ID = m1.post_id
";

$pageposts = $wpdb->get_results($querystr, ARRAY_A);

问题是此代码查询具有日期meta键的新旧帖子

注意: repeater _%_ date是ACF日期字段,将日期存储为Ymd(20190708)

请问有什么解决方法吗?

1 个答案:

答案 0 :(得分:0)

$get_featured_args3  = array(
              'post_type'  => 'post',
              'orderby'     => 'date',
              'order'       => 'DESC',
              'posts_per_page'=>-1,       
              'meta_query' => array(
                    'relation' ->'AND',
                    array(
                        'key'     => 'key1',
                        'value'   => 1,
                        'compare' => '=',
                    ),
                    array(
                        'key'     => 'key2',
                        'value'   => 1,
                        'compare' => '=',
                    ),
                ),
                'date_query' => array(
                    array(
                        'year' => date('Y'),

                    ),
                ),
            );
    $get_featured_results3 = new WP_Query( $get_featured_args3 );

    $posts = $get_featured_results3->posts;
    foreach($posts as $post){

    }