apply_filters_ref_array( ‘posts_results’, WP_Post[] $posts, WP_Query $query )

Filters the raw post results array, prior to status checks.

Parameters

$postsWP_Post[]
Array of post objects.
$queryWP_Query
The WP_Query instance (passed by reference).

More Information

The input of this filter is an array of posts, created by any(?) WP_Query looking for posts.

Source

$this->posts = apply_filters_ref_array( 'posts_results', array( $this->posts, &$this ) );

Changelog

VersionDescription
2.3.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example migrated from Codex:

    Code sample for filtering an array of posts results and return an array of post objects which does not have the word “selfie” in the title.

    add_filter( 'posts_results', 'my_posts_results_filter' );
    
    function my_posts_results_filter( $posts ) {
    	$filtered_posts = array();
    	print_r( $posts );
    	foreach ( $posts as $post ) {
    		if ( false === strpos($post->post_title, 'selfie')) {
    			// safe to add non-selfie title post to array
    			$filtered_posts[] = $post;
    		}
    	}
    	return $filtered_posts ;
    }

You must log in before being able to contribute a note or feedback.