add_filter posts_results
-
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');
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘add_filter posts_results’ is closed to new replies.