Skip to content

Instantly share code, notes, and snippets.

@norcross
Created July 23, 2012 14:50
Show Gist options
  • 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');
@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