Skip to main contentSkip to footer

I’m building a directory and would like the oldest posts to automatically display first in the search results, is this possible?

I found out how to add a link that the user can click to sort the results but would like an automatic solution. Can you provide code that can be added to the functions.php or search.php file to accomplish this?

You can easily change the order to using post date from the Relevanssi settings, but that sort is newest first.

Here’s a simple function that will change the order to post date, oldest first. Just add this code to your site:

add_filter( 'relevanssi_modify_wp_query', 'rlv_asc_date' );
function rlv_asc_date( $query ) {
    $query->set( 'orderby', 'post_date' );
    $query->set( 'order', 'ASC' );
    return $query;
}

(The question was asked here.)

Your account

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

Search

Popular Resources

Indexing custom post statuses
If you’re using custom post statuses, Relevanssi requires some tinkering. By default, Relevanssi only handles posts that are of status…
SearchWP Live Ajax Search

…One example is searching for attachments. By default, SearchWP Live Ajax Search doesn’t include any attachments in the search results. The post type is not a problem, but the post status is: SearchWP Live Ajax Search includes only posts with the post status publish, while attachments have a status of…Relevanssi doesn’t ship with a live search or autocomplete feature that would display results as the user types the search terms. There are many other plugins that provide this functionality, but few of these plugins work with Relevanssi. SearchWP Live Ajax Search is the best one that does. It’s very……to include private posts for the users who are allowed to see them. Just removing the post_status parameter will include drafts in the results. Normally drafts shouldn’t appear in searches, but since the SearchWP Live Ajax Search is an AJAX search, it is done in admin context, where drafts are…

Related Posts:

Comment Section:

3 Comments. Leave new

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