1

This code is not working

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

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

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

    }
}

I want to remove all results prior to jan 1 2013 so only posts after the 1/1/2013 will be shown in the results.

1
2

Note this problematic line:

$query->set( array( 'date_query' => array(

it should be

$query->set( 'date_query', array(

So try instead this form:

$query->set( 'date_query', 
    [
        [
            'after'     => 'January 1st, 2013',
            'inclusive' => true,
        ]
    ] 
);
2
  • Thanks. Why use square brackets instead of round brackets? – Dev May 28 '17 at 13:10
  • 1
    You're welcome. The [] is a shorthand notation for array() in PHP 5.4+. @Dev – birgire May 28 '17 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.