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.
Related Posts
Relevanssi Live Ajax Search
...accessibility. A basic search form looks something like this: <form role="search" method="get" class="search-form" action="/"> <label> <span class="screen-reader-text">Search for:</span> <input type="search" class="search-field" placeholder="Search …" name="s"> </label> <input type="submit" class="search-submit" value="Search"> </form> With Live Ajax Search, the form should look like this: <form role="search" method="get" class="search-form" action="/"> <label> <span class="screen-reader-text">Search for:</span> <input......type="search" class="search-field" placeholder="Search …" name="s" data-rlvlive="true" data-rlvparentel="#rlvlive" data-rlvconfig="default"> </label> <input type="submit" class="search-submit" value="Search"> <div id="rlvlive"></div> </form> Search results templates The Live Ajax Search plugin uses templates to display the search results. Using templates means it is easy to override the default search results from your theme if you want to......} ); Changes from SearchWP Live Ajax Search Relevanssi Live Ajax Search is a fork of SearchWP Live Ajax Search. I forked it from version 1.6.1 because it looked like SearchWP Live Ajax Search wasn’t getting updates anymore. I also wanted to improve the Relevanssi compatibility. Relevanssi Live Ajax Search...
Installing Relevanssi and adjusting the settings
...enabled, Relevanssi will index post excerpts. Expand shortcodes If this option is checked, Relevanssi will expand all shortcodes in post content before indexing the post. That means the shortcode content will be included in the index. This doesn’t always work as expected. If the shortcode provides dynamic content, Relevanssi will......indexing doesn’t work, one of the first things to try is to disable this option. Disable these shortcodes (Premium only) You can list unwanted shortcodes here: if a shortcode is causing problems in indexing, list it here and it will be disabled when Relevanssi indexes posts. Index user profiles (Premium......shows the number of hits in total. %score% shows the relevancy score of the post. %terms% shows how many hits each search term has. Try and see how each one of these works, %score% and %terms% are the most interesting keywords in most cases. Synonyms Synonyms tab Synonyms can be...
Related Posts
...Relevanssi uses a simple CSS grid to lay out the related posts. This setting determines the minimum width of each element. Experiment with different values to see what works. If you want to style the related posts, Relevanssi wraps everything inside div#relevanssi_related, the header is in div.relevanssi_related_grid_header, and the grid......itself is in div#relevanssi_related_grid. Each post is a div.relevanssi_related_post. If you want to make more extensive modifications, you can create your template. Relevanssi will look for the relevanssi-related.php template inside the current theme templates directory, and if it finds one, it will use that instead of the default template. When......<li class="relevanssi_related_post"> <a href="<?php echo get_permalink( $related_post_id ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"> <?php echo get_the_title( $related_post_id ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> </a> </li> <?php } ); ?> </ul> </div> <?php endif; Single post settings There are also Related posts controls on post edit pages. You can disable the auto-append for...
Category filter for search results pages
Sometimes it's nice to have a category filter on the search results pages: a simple dropdown where you can choose the category you want to show. It's easy to create one using wp_dropdown_categories() ...
Multisite searches
...everything else (sidebars, for example) works correctly, you need to restore the current blog. So, as the last thing inside the while loop, add: <?php restore_current_blog(); ?> Switching the blog without modifying the template If you can’t edit the search results templates for some reason, for example, if you’re using......you are using Toolset for displaying the search results, you’re going to have lots of trouble making multisite searching work. You can’t just switch the blog in the template, because Toolset doesn’t allow that, and besides the Toolset shortcodes wouldn’t necessarily work. Making this work will likely require rewriting Toolset......Toolset shortcodes wouldn’t necessarily work. Making this work will likely require rewriting Toolset shortcodes. If your use case is fairly straightforward, this may be feasible with a reasonable amount of effort. If possible, I would recommend just using regular WordPress templates, which are much easier to handle than Toolset Views....
Showing the number of search results
If you want to show the number of search results found on the search results template, you can find the value in the variable $wp_query->found_posts. Just add <?php echo '<p>Found ' . $wp...
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
The links are probably done dynamically, so if the date filter is active, the link is not added, just the text.
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?
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.
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!
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:
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?
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.
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’ )
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.
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)
Set “by_date” to whatever value is necessary, as described above.