Skip to content

Instantly share code, notes, and snippets.

@norcross
Created July 23, 2012 14:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norcross/3164028 to your computer and use it in GitHub Desktop.
Save norcross/3164028 to your computer and use it in GitHub Desktop.
filter search result to hide old posts
function rkv_search_filter( $where = '' ) {
// don't touch anything outside of a search query
if(is_admin() || !is_search() )
return $where;
// Hide posts older than 2 years old
if ( is_search() ){
$where .= "AND post_type = 'post' AND post_date >= '" . date('Y-m-d', strtotime('-730 days')) . "'";
return $where;
}
}
add_filter('posts_where', 'rkv_search_filter');
@anointed
Copy link

would this be a good place to use a transient? ( just learning more about transients this week, trying to figure out when/where to use them )

@norcross
Copy link
Author

Not really, because the search term would be different every time. You'd want to use a transient on a repeated query.

@anointed
Copy link

Thanks, that makes sense.

@norcross
Copy link
Author

Take a look at the code in this plugin: https://github.com/norcross/gist-sidebar-widget I use transients there to store the API call so I don't hit the limits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment