Is it possible to apply a meta_query to one specific post type in a query with multilple post types?

Question

I have two post types, news and events :

  • A news has no custom fields.
  • An event has two custom fields : start-date and end-date.

I want to display news and events on the same page, ideally with one single query.

One important fact is that events have to be ordered by end-date and that only future or current events have to be displayed.

Here’s the query args I am trying :

$args = array(
    'post_type' => array( 'news', 'events' ),
    'meta_key' => 'end-date',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_query' => array(
        array( 
            'key' => 'end-date',
            'value' => date ( 'Ymd' ),
            'type' => 'NUMERIC',
            'compare' => '>='
        )
    )
);

That works fine for events alone, but doesn’t when I add news to the post types.

The problem seems to be that I specified ‘meta_key’ and ‘meta_query’ parameters which are only present on events, as a result all the news are left out in the results.

So my question is, is there a way to target a meta_query to one specific post_type in one same query ?

0
, , mike23 4 years 2020-03-26T16:52:46-05:00 0 Answers 129 views 0

Leave an answer

Browse
Browse