admin管理员组文章数量:1295745
I'm trying to query two custom post types - I want all research_article
to return, but only events
that are in the future. My events are working as expected, but I'm not getting any research_article posts appearing. What's going wrong here?
$today = date( 'Y-m-d' );
$args = array(
'post_type' => array('research_article', 'events'),
'meta_key' => 'wpcf-date_time',
'post_status' => 'publish',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
array( // THIS ONE WORKS
'key' => 'wpcf-date_time',
'value' => $today,
'compare' => '>=',
'type' => 'DATE',
),
array(
'key' => 'pub_date', // THIS DOESNT WORK
'compare' => 'EXISTS'
),
'relation' => 'OR',
)
);
I'm trying to query two custom post types - I want all research_article
to return, but only events
that are in the future. My events are working as expected, but I'm not getting any research_article posts appearing. What's going wrong here?
$today = date( 'Y-m-d' );
$args = array(
'post_type' => array('research_article', 'events'),
'meta_key' => 'wpcf-date_time',
'post_status' => 'publish',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
array( // THIS ONE WORKS
'key' => 'wpcf-date_time',
'value' => $today,
'compare' => '>=',
'type' => 'DATE',
),
array(
'key' => 'pub_date', // THIS DOESNT WORK
'compare' => 'EXISTS'
),
'relation' => 'OR',
)
);
Share
Improve this question
asked Mar 21, 2021 at 23:23
RainyRainy
113 bronze badges
1
- Looks sensible to me. I'd probably try and find the SQL that WordPress is using, e.g. from the Query Monitor plugin, and work out what's wrong with it. – Rup Commented Mar 22, 2021 at 0:10
1 Answer
Reset to default 0So the issue seemed to be with my second meta_query array - instead of checking if pub_date EXISTS, I decided to check if wpcf-date-time DOESNT EXIST:
$today = date( 'Y-m-d' );
$args = array(
'post_type' => array('research_article', 'events'),
'posts_per_page' => 10,
'post_status' => 'publish',
'orderby' => 'meta_value date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'wpcf-date_time',
'value' => $today,
'compare' => '>=',
'type' => 'DATE',
),
array(
'key' => 'wpcf-date_time',
'compare' => 'NOT EXISTS'
),
'relation' => 'OR',
)
);
本文标签: Querying Two Custom Post Types with OR Not Working
版权声明:本文标题:Querying Two Custom Post Types with OR Not Working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741622887a2388912.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论