Support » Plugin: Relevanssi - A Better Search » [Plugin: Relevanssi] How to sort by Date?

  • Hi,
    i’m not sure how i can sort the result by Date?

    This is the code for the result page:

    <?php if (have_posts()) : ?>
    		<h3 class="pagetitle"><?php printf(__('Suchergebnisse: "%s"'), $s) ?></h3><br/>
    		<?php while (have_posts()) : the_post(); ?>	
        <div class="post" id="post-<?php the_ID(); ?>">
    			<?php $counter++; ?>
      <?php include(INCL.'/_post-header.php'); ?>
    			<div class="entry">
    				<?php the_content('Weiterlesen...'); ?>
    <?php include(INCL.'/_post-footer.php'); ?>
    </div> <br/><!--/post-->
    		<?php endwhile; ?>

    But where must i place the:


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


    Above the line with the while (have_posts()) bit, add:

    <a href="<?php bloginfo('url');?>?s=<?php echo get_search_query();?>&orderby=post_date&order=DESC">Order by date</a>

    That should do it.

    Thanks, but thats not what i want to do 🙂

    I want the result sorted by date by default. With out an link for the user.

    Plugin Author Mikko Saari


    In that case disable Relevanssi, as WordPress default search sorts by date – you don’t need a plugin for that. Relevanssi is designed to produce relevance-sorted results, not date-sorted results.

    Hi msaari,
    The default wordpress search is terrible 😉

    I’m using your Plug-In because the results are very good. But i want the combination from the good result and the order by date (by default).

    When i disable the plugin the results are bad…. No need to order that bad results…

    I just want to sort the relevannsi result by date by default and when a user wants to switch to the order by relevance i can add a hyperlink for that function. Like in your example above.

    It would be possible to change every code from search fields, but i think that’s not the best way.

    Plugin Author Mikko Saari


    Well, it’s fairly easy to fix. Find line 920 in the code, it looks like this:

    isset($wp->query_vars["orderby"]) ? $orderby = $wp->query_vars["orderby"] : $orderby = 'relevance';

    Change the ‘relevance’ to ‘post_date’. There you go. However, the results are good exactly because they are in the order of relevance. If you sort by date, you end up with something very much like the WordPress default search.

    Hi msaari,

    thanks, this is working very good. Also the results are much better than WP default search.

    Is it possible to note this “switch” as feature for one of the next versions?

    Best Regards,

    Hello Msaari-

    I, like the OP, have modified Relevanssi per your instructions above to sort results by descending post date.

    For me, sort by ‘relevance’ vs ‘post date’ depends in part on the styling of the search result posts themselves:

    – if search result posts look more like ‘bullet points’ (the Title alone, with minimal additional info), then sorting by ‘relevance’ looks and feels normal to me.

    – on the other hand: if search result posts have an ‘index.php’ feel to them (ie, Title, byline, thumbnail image, some content), a viewer may expect something resembling other similar looking lists: a descending-post-date order.

    Some last thoughts:
    – I also agree with the OP that Relevanssi, even sorting by post date, is far better than the WP default search.
    – a number of sites that I regard as ‘well designed’ sort search results by post date, I think that visitors to sites in general are comfortable with either sort order.
    – I also vote for a plugin panel option to choose ‘relevance’ or ‘post date’.

    Good work. Thanks.

    I also wanted to order the Relevanssi results by date, but didn’t want to change to core plugin, since the manual changes will be overwritten upon version update.
    What I did is, at the very top of the search.php template file, I added this –

    if ($_GET['orderby'] != 'post_date')	{
    	header("Location: ".get_bloginfo('url')."?s=".get_search_query()."&orderby=post_date&order=desc");

    In this way I just redirect the page to the orderby needed.

    Do you find this a good practice to use?

    I see that although using the GET parameters &orderby=post_date&order=desc in the url, the search results sometimes does not being displayed by this orderby.
    Do you have an idea what could be the reason for that?

    Plugin Author Mikko Saari


    Sorry, no, that should do the trick.

    You don’t have to use header, though, it should be enough to add hidden input fields “orderby” and “order” in the search form.

    Thanks, I will change the header location to hidden input fields. I think it is a better practice.
    But anyway, why is that for some searches I do get them ordered by post_date as expected, but for some other search strings, the results are not ordered by post_date? The orderby post_date appears to not fully be working.

    Plugin Author Mikko Saari


    I don’t know — by all means it should work.

    Could you check this issue for a premium license?

    Plugin Author Mikko Saari


    It works on, which has a recent version of Premium running.

    I meant to ask if you could support that in the specified site, if it will run premium version.

    Anyway, it does work most of the time orderby the post_date, but sometimes it just doesn’t..

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘[Plugin: Relevanssi] How to sort by Date?’ is closed to new replies.