• Resolved c3iqwp

    (@c3iqwp)


    I want to filter the results of a search. I need to examine the result set and remove some posts based on the existence of others. I beleive that the filter posts_results is the correct filter to use but when I dump the $posts array I only get one object. That happens to be the only embedded video on the site and not a post.

    I changed the filter to return an empty array but it doesn’t change the results on the search page. This is from the codex example. I stopped at the point where the ‘error_log(print_r($posts, true));’ returns just one result where the search page displays several results.

    function preferCustomContentFilter($posts) {
      $filtered_posts = array();
      error_log(print_r($posts, true));
      foreach ($posts as $post) {
    error_log("HERE");
        if (false === strpos($post->post_title, 'selfie')) {
        // safe to add non-selfie title post to array
        $filtered_posts[] = $post;
        }
      }
      //return $filtered_posts ;
    
      // try to affect the search/results page
      return array();
    }
    add_filter('posts_results', 'preferCustomContentFilter');
    • This topic was modified 6 years, 2 months ago by c3iqwp. Reason: Added code marks
    • This topic was modified 6 years, 2 months ago by c3iqwp.
Viewing 1 replies (of 1 total)
  • Thread Starter c3iqwp

    (@c3iqwp)

    I needed to look at ‘the_posts’ rather than ‘post_results’ and the add_filter needed a lower priority:

    add_filter('the_posts', 'preferCustomContentFilter', 1000);

Viewing 1 replies (of 1 total)
  • The topic ‘add_filter posts_results’ is closed to new replies.