Forums Forums Search & Filter Pro Custom display results with WP_Query that contains ‘post__in’

Viewing 6 posts - 1 through 6 (of 6 total)
  • Anonymous
    #249954

    Hello, I’m trying to implement filtering on set array of posts defined in WP_Query parameters. The WP_Query works (the right posts show up) without Search & Filter Pro, but when I add it into the mix, the WP_Query displays ALL ‘posts’ not the posts defined in the ‘posts__in’ array. Am I missing something? Thanks in advance for your time.

    
    $favposts = $user->simplefavorites[0]['posts'];
    // $favposts returns as: 
    // Array ( [0] => 201 [1] => 194 [2] => 196 [3] => 191 [4] => 199 [5] => 202 )
    
    $args = array(
       'post_type' => 'post',
       'post__in'  => $favposts
    );
    $args['search_filter_id'] = 377;
    $query = new WP_Query($args);
    
    // The Loop
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post();
    		
    		?><div class="card"><?php the_title(); ?></div><?php
    		
    	}
    } else {
    	// no posts found
    }
    
    Trevor
    #249964

    Unfortunately, our plugin will override your post__in setting, as we use that to send the final list of IDs to the database. That means you cannot use it.

    Anonymous
    #249986

    Hrm, bummer. Is there some other work around to programmatically search a limited array of posts?

    Ross Moderator
    #250014

    Hmmm, you could use a really late pre_get_posts, check what is in post__in, and array_intersect with your array?

    Just a theory.

    In a pre_get_posts, you can check $query->get('search_filter_id') to check if the query is a S&F query.

    Thanks

    Anonymous
    #250128

    Thanks for the theory and your support! I’ll investigate and report back if I’m able to get it to work.

    …to be continued.

    Ross Moderator
    #250208

    Great stuff, let me know.

Viewing 6 posts - 1 through 6 (of 6 total)