WordPress.org

Forums

[resolved] Search Query - Exclude Custom Posts where Cusom Field Value is Yes (5 posts)

  1. vbkun
    Member
    Posted 1 year ago #

    I`m having a bit of trouble solving this out, I have:

    - Custom post types (aves, mamiferos, repteis)
    - A custom field (mostrar_no_site), this field value is either "sim" or "nao".

    I need to exclude from search results the posts where the value for that custom field is 'nao'.

    I'm working with the pre_get_posts hook, but I just can't this work so far, whatever i try returns me no results at all. -.- the code:

    function search_filter($query) {
        if ( $query->is_search ) {// Make sure it is a search page
    
    		$meta_query = array(
    						 array(
    							'key'=>'mostrar_no_site',
    							'value'=>'nao',
    							'compare'=> '!=',
    						 ),
    		);
    
    		$query->set('meta_query',$meta_query);
    
    		print_r( $query );
    
    		return $query;
    	}
    }
  2. Craig Ralston
    Member
    Posted 1 year ago #

    I believe this is going to require a UNION statement which WP_Query does not support. This is going to involve either using multiple queries or writing your own SQL.

    Trying to query all post types but restrict results for only the one CPT in the same query is not going to work. I can provide a generic example, though it is not the most ideal, it is the only way I was able to accomplish this.

    $post_ids = new WP_Query(array(
        'fields' => 'ids',   // query for one post type
                             // other parameters
    ));
    
    $post_ids_2 = new WP_Query(array(
        'fields' => 'ids',   // query for the others
                             // other parameters
    ));
    
    // join them
    $post_ids = $post_ids->posts + $post_ids_2->posts;
    $posts_qry = new WP_Query(array('post__in'=> $posts_ids));

    I couldn't find the original post that I came across when I encountered this problem but someone had assisted me on Stack Overflow with this same issue.

    Hope that helps some.

  3. vbkun
    Member
    Posted 1 year ago #

    I was trying a step-by-step approach, since i never dealt with the pre_get_posts before, so I was trying to filter using the custom post only and then move to next step (how would i merge results/solve the situation 'posts that doesn't have this field).

    Oddly I couldn't even get the results from the posts that had values for that field. So, adding this experience to your comment, I guess the query just doesn't work at all with that combination since it wasn't built for that.

    Then from your example I understand that the object from WP_Query() is equal to the initial query object, in structure. So then i could do something like:

    function search_filter($query) {
    
    	if ( $query->is_search ) {
    
           //all WP_Query() stuff here 
    
    		return $query;
    
    	}
    }
    
    add_filter('pre_get_posts', 'search_filter');

    To use WP_Query to modify the search query trought pre_get_posts hook?

    (from your example I think yes but I'm not sure if I understood you correctly);

  4. Craig Ralston
    Member
    Posted 1 year ago #

    To use WP_Query to modify the search query trought pre_get_posts hook?

    That is what I was getting at, yes. The fact that you need to run more than one query to get your result is what makes the solution a bit less than ideal but that is how I do it for a lack of anything more efficient.

    1 query for all posts
    1 query for your CPT value exclusion
    Then join them for your modified query and you should see the result you are seeking - All post types + your CPT exclusion

  5. vbkun
    Member
    Posted 1 year ago #

    Thanks Craig,

    I didn't knew I could actually the WP_Query with the pre_get_posts hook, got it that I gotta merge them, did something similar before, this part should not be an issue here so I guess this is solved.

    Thanks again.

Topic Closed

This topic has been closed to new replies.

About this Topic