- Home
- About
- Archives
- My WordPress Plugins
- Bulk Change Attachment Parent
- Clean Archives Reloaded
- Disable Trackbacks
- jQuery Lightbox For Native Galleries
- Local Time
- Regenerate Thumbnails
- Registered Users Only
- SyntaxHighlighter Evolved
- Viper’s Video Quicktags
- WordPress Admin Bar
- WordPress Download Counter
- WordPress.org One-Click Install
- YOURLS: Short URL Widget
- Other Plugins
- Discontinued Plugins
- Donate
- Contact
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 = '' ) {
$where .= " AND post_date > '" . date( 'Y-m-d', strtotime('-30 days') ) . "'";
return $where;
}
add_filter( 'posts_where', 'last_thirty_days' );
$some_posts = get_posts( array( 'suppress_filters' => false ) );
?>
| Print article | This entry was posted by Alex (Viper007Bond) on February 2nd, 2010 at 4:29 AM, and is filed under Coding, WordPress. Follow any responses to this post through RSS 2.0. You can leave a response or pingback from your own site. |
about 5 months ago
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..
about 5 months ago
In Reply To Jim Gaudet:
You wouldn’t use this code to do that. You would want something like this:
<?php if ( get_the_time('U') < strtotime('-30 days') ) { // show ads } ?>about 5 months ago
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,
about 4 months ago
care to explain more about ‘suppress_filters’ ? what is it ?
about 1 month ago
Thanks for the tip
about 1 month ago
In Reply To uwiuw:
It’s one of the function’s optional arguments. Setting it to
falsewill disable the function from not using any filters on it’s query and results like it normally does.