How to Display Search Results After a Specific Date in WordPress

By ThemesDNA1 min read

Share:

To display search results after a specific date in WordPress, you can customize your theme's search query using the "pre_get_posts" action hook. This approach guarantees the display of only posts published after the specified date in the search results.

To filter search results after the specified date, add the following code to the functions.php file of your theme:

function themesdna_filter_search_results_by_date( $query ) {
    if( $query->is_main_query() && ! is_admin() && $query->is_search() ) {
        // Set the date to filter results after
        $after_date = '2022-04-20';

        $date_query = array(
            array(
                'after' => $after_date,
                'inclusive' => true, // Include posts published on the specified date
            ),
        );

        $query->set('date_query', $date_query);
    }
}
add_action( 'pre_get_posts', 'themesdna_filter_search_results_by_date' );

Note: Remember to replace "2022-04-20" with your desired date.

Premium WordPress themes One-time payment   /   Lifetime updates

Ready to Build?

Get the ThemesDNA bundle.

Get all premium ThemesDNA themes with lifetime updates and unlimited website usage for a one-time payment.

  • 56 premium themes
  • Lifetime updates
  • Unlimited websites
  • No renewals

The complete premium theme bundle

$75

One payment. No renewals.

Get the ThemesDNA Bundle — $75