WordPress: Using Filters With get_posts()

Something I just learned and thought I’d share to save others the trouble: if you’re attempting to filter the results of get_posts(), for example with the posts_where filter, you need to disable suppress_filters.

For example, here’s how to fetch some posts from within the past 30 days (based on an example from the Codex):

<?php

function last_thirty_days( $where = '' ) {
	global $wpdb;

	$where .= $wpdb->prepare( " AND post_date > %s", date( 'Y-m-d', strtotime('-30 days') ) );

	return $where;
}

add_filter( 'posts_where', 'last_thirty_days' );

$some_posts = get_posts( array( 'suppress_filters' => false ) );

// Important to avoid modifying other queries
remove_filter( 'posts_where', 'last_thirty_days' );

?>

12 comments to WordPress: Using Filters With get_posts()

  1. Jim Gaudet says:

    This is great, for me I am going to use something like this to only show ads on posts that are of a certain age..

  2. Jim Gaudet says:

    Sweet, thanks! I knew it wasn’t right, but it got me thinking of what I wanted to do and then you did it for me,

    Much appreciated,

  3. uwiuw says:

    care to explain more about ‘suppress_filters’ ? what is it ?

    • Viper007Bond says:

      It’s one of the function’s optional arguments. Setting it to false will disable the function from not using any filters on it’s query and results like it normally does.

  4. Mariano says:

    Thanks for the tip :)

  5. Andy Bailey says:

    this was just enough information to get me in trouble!

    I ended up having filters for posts_where, post_limits, posts_orderby, posts_join, posts_groupby all working together in my plugin!

    thanks for the post, it really helped!

  6. larf2k says:

    Oh man! I was pulling hairs out for an hour over this until I found your post.

    Thanks so much, this allows for so many possibilities. Altering the where clause is really powerful.

    Question though: if I only want it to apply to one get_posts() and no others, or, say have different get_posts()’s on different parts of the theme have different where filters, should I do something like:

    function filter_where( $where = '' ) {
    	// posts in the last 30 days
    	$where .= " AND post_date > '" . date('Y-m-d', strtotime('-60 days')) . "'";
    	return $where;
    }
    add_filter( 'posts_where', 'filter_where' );
    			$posts = get_posts(array(
    				'suppress_filters' => false,
    				'numberposts' => -1,
    				'category__not_in' => explode(',',$subscriber->categories_excluded),
    				'post_type' => $post_types_included
    				)
    			);
    remove_filter( 'posts_where', 'filter_where' )
    

    (removing the filter so it doesn’t affect other suppress_filter get_posts()’s)

    I’m thinking this is necessary otherwise this filter will be carried out sitewide…

  7. [...] to Viper007Bond for, quite literally, showing me how to do [...]

  8. Gabriel Dancause says:

    I’m trying to get the 6 previous posts in my single.php in relation to the post being displayed. Would you have an idea on how to use your code to achieve that task?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

If you wish to post code, write it like [code]blah[/code] so it will display properly.