1

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 ?

  • Have you tried with the relation parameter, link to the docs?. You could run into problems with the orderby – Tomás Cot Sep 4 '14 at 22:12
  • Indeed, as I order by a meta field that exists only on events, all news are discarded from the results. – mike23 Sep 5 '14 at 12:32
  • why not create 2 loops? – Tomás Cot Sep 5 '14 at 12:34
  • I'd like to have one loop so I can show the two kinds of posts in the same list with pagination. – mike23 Sep 9 '14 at 7:31
3

You can add another array into your meta_query to include all posts from your second post type.

Given in your example, you have 2 post types

  1. events
  2. news

If you can pick a meta_key that only exists for news but doesn't exist on events, then this set of args would return all news post types and still filter on events.

$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' => '>='
        ),
        array(
          'key'      => 'my_unique_key_for_news',
          'compare'  => 'EXISTS'
        ),
        'relation' => 'OR',
    )
);

The relation => OR is important. It says please return results that fit your end-dates that are >= today, OR all the items that match up on the key for news (which would be all news items).

This way you are getting specific about the meta_values for events, but returning all results for news, and thus the filter leaves the second post type alone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.