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]<?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’ );
?>[/php]
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..
You wouldn’t use this code to do that. You would want something like this:
[php]<?php
if ( get_the_time(‘U’) < strtotime(‘-30 days’) ) {
// show ads
}
?>[/php]
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,
Pingback: WordPress Picks for the Week [02/03] | Techtites
care to explain more about ‘suppress_filters’ ? what is it ?
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.Thanks for the tip 🙂
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!
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:
[php]
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’ )
[/php]
(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…
Yeah, that’s the best way to do it. Add it, query, and then remove it.
Pingback: WordPress - Showing Ads only on Old Posts
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?
I like this code a lot, but I am trying to add the filter inside a function and it doesn’t seem to be working, even if I remove the conditional. Is that something WordPress doesn’t allow for?
[code language=”php”]
function sr_get_submenu( $args, $sr_last_fourteen_days = false, $image_size = ‘small_thumb’ ) {
$output = ”;
if($sr_last_fourteen_days)
add_filter( ‘posts_where’, ‘sr_last_fourteen_days’ );
$posts = get_posts( $args );
……..
if($sr_last_fourteen_days) remove_filter( ‘posts_where’, ‘sr_last_14_days’ );
}
function sr_last_fourteen_days( $where = ” ) {
global $wpdb;
$where .= $wpdb->prepare( " AND post_date > %s", date( ‘Y-m-d’, strtotime(‘-14 days’) ) );
return $where;
}
[/code]
That should work fine. Does
$argscontainsuppress_filters? 😉[code language=”php”]
// …
$args = wp_parse_args( $args );
$args[‘suppress_filters’] = false;
$posts = get_posts( $args );
// …
[/code]
You nailed it. That makes me feel dumb, but thanks a ton for the help.
Thank you for your tips. It helps me a lot 🙂
Thanks. I was wondering why the results weren’t filtered. So by default get_posts removes filters. I learned something !
Great tip!
But, how to add the suppress_filters to the_content or the_excerpt later? Imma getting strange chars displayed… :_(
Best!
suppress_filtersis a parameter forget_posts(). It doesn’t apply to other functions. Your issue is unrelated.Thanks you very much bro that help’s me alot
Thanks you so much. It helped me a lot. I really appreciate your efforts and for sharing.
Pingback: Welcart??????????????? | Welcart ??????
Pingback: WordPress Picks for the Week [02/03] | Techtites
It’s 2022 and this post helped me a lot. Thank you#!
I tried to make custom fields searchable, and it worked fine everywhere, but not when using get_posts(). I first thought, that maybe “is_search” is not set true. Then i found out that the filters don’t get applied and found your post.
2 hours of debugging ^^
Thanks. I was wondering why the results weren’t filtered. So by default, get_posts removes filters. I learned something!
Hello,
I hope all is okay with you. My outreach campaign led me to your website, which I discovered via Google.
I’m interested in having a few posts published on your website, and I’ll pay you via PayPal or Payoneer to host my original content.
I will give you high-quality material for your website thanks to my staff of US writers.
Please let me know how much you charge for each post.
Hopefully speaking with you soon.