1

I have custom post type called event, which has custom fields for start_date and end_date. For the event archive, I'm filtering out "expired" events using the following meta query added to the main query inside a pre_get_posts filter, which works fine:

if ( is_post_type_archive( 'event' ) ) {
    $query->set( 'meta_query', array(
        'relation' => 'OR',
        array(
            'key' => 'start_date',
            'value' => $today = date( 'Ymd' ),
            'compare' => '>='
        ),
        array(
            'key' => 'end_date',
            'value' => $today = date( 'Ymd' ),
            'compare' => '>='
        ),
    ) );
}

I'd like to do the same for search queries. Is it possible to add a meta query to the main query and have it only apply to a certain post type (i.e. event)? Pseudocode (I know this doesn't work, but to illustrate):

if ( is_search() ) {
    $query->set( 'meta_query', array(
        'relation' => 'OR',
        array(
            'key' => 'post_type',
            'value' => 'event',
            'compare' => '!=',
        ),
        array(
            ....
}

2 Answers 2

4

In the case that the start_date custom field only exists for the event post type, this works:

$query->set( 'meta_query', array(
    'relation' => 'OR',
    array(
        'key' => 'start_date',
        'compare' => 'NOT EXISTS',
    ),
    array(
        'key' => 'start_date',
        'value' => $today = date( 'Ymd' ),
        'compare' => '>=',
    ),
    array(
        'key' => 'end_date',
        'value' => $today = date( 'Ymd' ),
        'compare' => '>=',
    ),
) );

By comparing the start_date key to NOT EXISTS, the other post types are not filtered out. Obviously this is not an ideal solution, so I'm still curious if there are better ideas out there.

2
  • 1
    This is pretty much as much as you can do with what Wordpress has to offer. This is exactly what I had in mind, just did not have time to put it into code. The only other way I see of doing this is with a custom SQL query which can get quite messy, and you loose all your filters as well. Jun 17, 2015 at 10:27
  • Allright, thanks for the comment @PieterGoosen. Good to know! Jun 17, 2015 at 10:33
1

Your given answer is pretty smart!!

Another way I think you could accomplish the same thing and be a little bit more specific would be to pick a new key that exists only on the second post type and compare with EXISTS.

So your second post type is search (not exactly sure from your example). Your first post type is event. If your main query is searching through both post types, then this meta_query would include all posts of the second post type.

$query->set( 'meta_query', array(
'relation' => 'OR',
    array(
        'key' => 'my_exclusive_meta_key_for_my_other_post_type',
        'compare' => 'EXISTS',
    ),
    ...
) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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