Skip to main contentSkip to footer

Ordering search results by date

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

Ultimate FAQs

…*/ function rlv_no_ufaqs( $query ) { if ( EWD_UFAQ_FAQ_POST_TYPE === $query->query_vars[‘post_type’] ) { return $query; } if ( ! empty( $query->query_vars[‘post_type’] ) ) { $query->query_vars[‘post_type’] .= ‘,’; } $query->query_vars[‘post_type’] .= ‘-‘ . EWD_UFAQ_FAQ_POST_TYPE; return $query; } Add this to your site. This function will add the FAQ post type as……function will make Relevanssi not * run the search. * * @param bool $do_stuff Should Relevanssi functions do the things they do? * @param WP_Query $query The WP_Query object. */ function rlv_ufaq_query( $do_stuff, $query ) { if ( EWD_UFAQ_FAQ_POST_TYPE === $query->query_vars[‘post_type’] ) { $do_stuff = false; } return $do_stuff; }……a negative post type unless the search is explicitly asking for the FAQ post type. Not using Relevanssi Another option is to block Relevanssi out of the FAQ queries. Here’s how you do that: add_filter( ‘relevanssi_prevent_default_request’, ‘rlv_ufaq_query’, 10, 2 ); add_filter( ‘relevanssi_search_ok’, ‘rlv_ufaq_query’, 10, 2 ); /** * Makes Relevanssi

Indexing with a cron job
I have some trouble with reindexing of new posts. The reason is simple – I sync data into the database…

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