Skip to main contentSkip to footer

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.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Popular searches

…that. So, for these two reasons Relevanssi doesn’t ship with a “popular searches” widget. Creating one in itself would not be difficult. This code snippet, for example, creates a very simple popularsearches shortcode. The shortcode will display the five most common search terms. add_shortcode( ‘popularsearches’, function() { global $wpdb, $relevanssi_variables;……$table = $relevanssi_variables[‘log_table’]; $popular_searches = $wpdb->get_col( “SELECT query FROM $table GROUP BY query ORDER BY COUNT(*) DESC LIMIT 5” ); return ‘<ul><li>’ . implode( ‘</li><li>’, $popular_searches ) . ‘</li>’; } ); In production, this should have some kind of a profanity filter. You’d also want to put in place some…This is a very common request: “How do I show the most popular searches in a widget or on a page?” Relevanssi does not have such a feature built in, and the reason for that is twofold: First, on many sites, the most common searches are either spam or boring….

Yoast Local SEO
Yoast Local SEO plugin breaks Relevanssi search. It enhances the search by adding meta queries. Unfortunately they don’t work with…
Total

Total is a popular theme from WPExplorer. It mostly works fine with Relevanssi, with just one little issue. Total sets the post_type parameter for all searches to include all public post types. This is fine for most cases, but if you want to include user profiles or taxonomy terms in…

Related Posts:

Comment Section:

11 Comments. Leave new

  • 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)

    Reply
  • 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.

    Reply
  • 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.

    Reply
    • 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’ )

      Reply
  • 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:

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

    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?

    Reply
  • 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!

    Reply
  • 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.

    Reply
  • 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?

    Reply
  • The links are probably done dynamically, so if the date filter is active, the link is not added, just the text.

    Reply
  • 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

    Reply

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed