• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2662

Original Genesis Tutorials & 5000+ Guaranteed Code

Snippets

  • Support
  • Newsletter
  • Videos
  • Log in

Premium Member? - Request custom code

Return Search Results After A Specific Date

If you want to exclude older posts from your search results, you can using pre_get_posts.

The following code uses the date_query from WP_Query.

Only returns results after the 1/1/2013 using the WordPress search function.

add_filter( 'pre_get_posts', 'search_filter' );
function search_filter($query) {

if ( ! is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {

$query->set( 'date_query', 
    [    [
    'after'     => 'January 1st, 2013',
    'inclusive' => true,
    ]    ] 
    );
    }
  }
}

The code works with the WordPress search function and search widget included in all WordPress installations.

pre_get_posts

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

PHP Code

template_include

get_body_class

if else

array

class_exists

foreach

sprintf

add_action

printf

variable

Advertise · WPEngine · Genesis · Log in

  • How Premium Membership Works
  • Sign Up
  • Support
  • Subscription Details/Invoice
  • Tagged Tutorials
  • Access-Download Problems