Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use posts_results filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
September 25, 2022
5 minutes read

Alrighty, let’s dive into the posts_results filter! As an Indian developer, I can tell you this filter is super handy when you want to tweak the raw post results array before WordPress does its status checks. Trust me, using this properly can make a world of difference in how your site handles post queries.

To get started with the posts_results filter, you’ll need to register it using add_filter. You can drop this code into the functions.php file of your active theme or, better yet, create a custom WordPress plugin. We at WePlugins always recommend creating a custom plugin to avoid any issues when updating your theme.

In the examples below, we’ve defined a function called modify_posts_results_defaults which takes two parameters. We’ve registered this function using add_filter. The first parameter posts_results is the name of the hook, the second parameter modify_posts_results_defaults is the name of the function that needs to be called, the third parameter is the priority, and the last parameter is the number of arguments (if any) to be passed to the registered function.

Sometimes, you might want to remove a registered hook. You can use remove_filter to remove the posts_results filter.

Parameters

    Below are the 2 parameters required to use this hook:

  • $posts: (WP_Post[]) Array of post objects.
  • $query: (WP_Query) The WP_Query instance (passed by reference).

Live Example 1

Here’s a basic example of filtering posts that do not have “selfie” in the title:

add_filter( 'posts_results', 'weplugins_my_posts_results_filter' );
function weplugins_my_posts_results_filter( $posts ) {
	$filtered_posts = array();
	foreach ( $posts as $post ) {
		if ( false === strpos($post->post_title, 'selfie')) {
			$filtered_posts[] = $post;
		}
	}
	return $filtered_posts;
}

Live Example 2

Below is an example of how you can use this hook to modify the $posts variable conditionally:

function weplugins_modify_posts_results_defaults($posts, $query) {
	// Update the $posts variable according to your website requirements and return this variable
	return $posts;
}
// Add the filter
add_filter( "posts_results", "weplugins_modify_posts_results_defaults", 10, 2 );

Live Example 3

To remove a hook callback, use the example below:

remove_filter( "posts_results", "weplugins_modify_posts_results_defaults", 10, 2 );

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Access Premium WordPress Plugins

Contact Us

If you need any customization or run into any issues using this hook, feel free to contact us. We’d be happy to assist you!

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.