0

I've been unable to find anything on how to do this, or if it's even possible.

What I want to do is not display results of search in custom post type "resources" on which "resource_usertype" (key) is not "Public" or "Students" or "Alumni" (values).

Or maybe a better way to say it is to only display search results in which values of "resource_usertype" are "Public", "Students", or "Alumni".

2 Answers 2

1

You need to implement a hook for pre_get_posts filter, in which you can set what you need. Pay attention that you should change only search query, so you have to check if is_search() method returns true. The hook should look like this:

add_filter( 'pre_get_posts', 'wpse8170_pre_get_posts' );
function wpse8170_pre_get_posts( WP_Query $query ) {
    if ( $query->is_search() ) {
        $query->set( 'post_type', array( 'resources' ) );
        $query->set( 'meta_query', array(
            array(
                'key' => 'resource_usertype',
                'value' => array('Public', 'Students', 'Alumni'),
                'compare' => 'IN',
            )
        ) );
    }

    return $query;
}
1
  • Eugene - this looks like it would work, but it actually excludes all results on all post types and pages.
    – Ray Gulick
    Feb 13, 2013 at 22:25
0

It's perfectly possible!

$args = array(
   'post_type' => 'resources',
   'meta_query' => array(
       array(
           'key' => 'resource_usertype',
           'value' => array('Public', 'Students', 'Alumni'),
           'compare' => 'IN',
       )
   )
 );
 $query = new WP_Query($args);
1
  • This is a query for listings, isn't it?. Specifically trying to exclude from search results.
    – Ray Gulick
    Feb 13, 2013 at 22:22

Your Answer

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

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