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',
)
);
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
So 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',
)
);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0