Support » Plugin: Relevanssi - A Better Search » Possible to show search results in ascending order by post date?

  • Resolved Mark

    (@markob17)


    Hi,

    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?

    Thank you so much!

    mark

    https://wordpress.org/plugins/relevanssi/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Mikko Saari

    (@msaari)

    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;
    }

    Add this to functions.php to get posts sorted oldest first.

    Hi Mikko,

    Thanks for sending over the code, however, it returns the error below:

    Parse error: syntax error, unexpected T_FUNCTION

    Any ideas?

    Specifically, the line in the error was referring to this section of the code:

    function rlv_asc_date($query) {

    Plugin Author Mikko Saari

    (@msaari)

    Sounds like a typo somewhere, for example a missing semicolon on the first line might cause an error like that. It seems correct to me, so make sure it was copy-pasted correctly. Instead of copy-pasting, you could also type it yourself in case there’s a shift-space or something like that in the code I wrote.

    Mikko, thank you for your quick support on this. Turns out I had to first disable w3 total cache before adding it to my site plugin. Not sure why but after enabling w3 total cache again it now works without issue.

    Thanks again!

    Plugin Author Mikko Saari

    (@msaari)

    Weird. Glad you got it sorted out!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Possible to show search results in ascending order by post date?’ is closed to new replies.