Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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".

share|improve this question
add comment

2 Answers

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;
}
share|improve this answer
    
Eugene - this looks like it would work, but it actually excludes all results on all post types and pages. –  Ray Gulick Feb 13 '13 at 22:25
add comment

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);
share|improve this answer
    
This is a query for listings, isn't it?. Specifically trying to exclude from search results. –  Ray Gulick Feb 13 '13 at 22:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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