Posted on

Filter search results by date

You can specify date limits on searches with by_date search parameter. Here’s a link you can add to your search results page to offer your visitor the ability to restrict the search results to last day:

<?php echo '<p><a href="' . get_bloginfo('url') . '?s=' . get_search_query() . '&by_date=1d">results from the last day</a></p>'; ?>

The date range is always back from the current date and time. Possible units are hour (h), day (d), week (w), month (m) and year (y). So, to see only posts from past week, you could use by_date=7d or by_date=1w.

Using wrong letters for units or impossible date ranges will probably lead to no results at all, depending on case. If there’s a number present, but the letter makes no sense, Relevanssi will assume the unit is days.

Thanks to Charles St-Pierre for the idea.

11 comments Filter search results by date

  1. Hi,

    Thanks for the great plugin – it is a life saver for a project I am working on – one question; on the example you provide above when you click on a filter it disables the link and puts the text in bold, are you able to advise on how this is completed?

    Best,

    Mike

  2. Thanks for the response Mikko – I reasoned that this is probably what it is doing, though have no idea how I would complete it on my site… Don’t suppose you would be able to provide an example of the code this would require?

  3. Sorry, not my site so I don’t have code ready and actually don’t know how it was done. But I assume he checks the by_date query variable from $wp_query->query_vars[‘by_date’] or so, and adjusts accordingly.

  4. I have found that the search as standard returns only newly created posts when using these filters, for my use I required this to cover new and updated posts (knowledge base) so I have modified this code in relevansi.php:

    WHERE post_date> DATE_SUB(NOW(), INTERVAL $n $unit))”;

    to:

    WHERE post_modified > DATE_SUB(NOW(), INTERVAL $n $unit))”;

    This means the search returns new and modified posts when using the date filters rather than just based on the created date.

    (FYI – post_date and post_modified are column headers in the SQL tables not wordpress references)

    Maybe of use to some of you!

  5. Hi, in case it is useful for somebody, I have managed to turn the code suggested here, which provides after-search filtering, into a selection box before-search. I have added this to the searchform code:

    &lt;div id="search_filter"&gt;
    Date filter:
            &lt;select id="by_date" class="postform" name="by_date"&gt;
                &lt;option class="level-0" value="7d"&gt;Last week&lt;/option&gt;
                &lt;option selected="selected" value="1m"&gt;Last month&lt;/option&gt;
                &lt;option class="level-2" value="6m"&gt;Last 6 months&lt;/option&gt;
                &lt;option class="level-3" value="1y"&gt;Last year&lt;/option&gt;
                &lt;option class="level-1" value="all"&gt;All dates&lt;/option&gt;
            &lt;/select&gt;
    &lt;/div&gt;

    It works as far as I have testd. Now, if someone could help… Can you think of any way to show the option you selected in the results page?

  6. The chosen option is in $wp_query->query_vars[‘by_date’]. Dig it from there. You can easily set the option selected according to the previously set value.

    1. Thank you. Sorry if I’m being dense, but I have tried three ways of getting that, and all of them show nothing:
      1. echo $wp_query->query_vars[‘by_date’]
      2. print_r ( $wp_query->query_vars ) It prints the whole array, but no by_date
      3. $filter = get_query_var( ‘by_date’ )

  7. Hmm… Actually, Relevanssi doesn’t introduce ‘by_date’ query variable. But why does it work, then? Strange.

    Anyway, try finding function relevanssi_query_vars() and adding “$qv[] = ‘by_date’;” to the function. I think it should work once you do that.

  8. Hi,

    how can i filter by date when directly accessing the search engine via relevanssi_do_query($your_query_object)?

    I have a news page and plan to use relevanssi for display of related content (already implemented but stuck with the date problem). It makes no sense to show 3 year old news as related content …

    (Yes, i tried the wpQuery ‘date_query’, it has no effect on the searchresult)

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *