Forums

Relevanssi - A Better Search
How to sort by Date? (24 posts)

  1. Schnappinator
    Member
    Posted 1 year ago #

    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...'); ?>
    			</div><!--/entry-->
    <?php include(INCL.'/_post-footer.php'); ?>
    </div> <br/><!--/post-->
    
    		<?php endwhile; ?>

    But where must i place the:
    'orderby=post_date&order=DESC'

    Thanks,
    Manuel

  2. msaari
    Member
    Posted 1 year ago #

    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.

  3. Schnappinator
    Member
    Posted 1 year ago #

    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.

  4. msaari
    Member
    Posted 1 year ago #

    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.

  5. Schnappinator
    Member
    Posted 1 year ago #

    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.

  6. msaari
    Member
    Posted 1 year ago #

    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.

  7. Schnappinator
    Member
    Posted 1 year ago #

    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,
    Manuel

  8. billseymour
    Member
    Posted 1 year ago #

    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.

  9. maorb
    Member
    Posted 1 year ago #

    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?

  10. maorb
    Member
    Posted 1 year ago #

    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?

  11. msaari
    Member
    Posted 1 year ago #

    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.

  12. maorb
    Member
    Posted 1 year ago #

    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.

  13. msaari
    Member
    Posted 1 year ago #

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

  14. maorb
    Member
    Posted 1 year ago #

    Could you check this issue for a premium license?

  15. msaari
    Member
    Posted 1 year ago #

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

  16. maorb
    Member
    Posted 1 year ago #

    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..

  17. maorb
    Member
    Posted 1 year ago #

    I changed the header("Location: to hidden input fields and now both orderby working and also searching words with " and ' .
    Somehow, it appears that the header redirect did something wrong to the search.

  18. msaari
    Member
    Posted 1 year ago #

    Well, the Premium license does include support, so if you're having problems, I can definitely check to see if I can fix them.

  19. jason_bassford
    Member
    Posted 11 months ago #

    I just came to this post because I needed exactly the same thing - to sort results by date, not relevance. For one of my sites, relevance is determined by the post of the date. The further back in time, the less the importance of the information. So, for that site, relevance sorting is not why I use Relevanssi. Rather, I use it for its regex abilities and the number of results that are returned.

  20. msaari
    Member
    Posted 11 months ago #

    Well, the solution has already been mentioned in the thread.

    In Premium it's possible to build a filter that gives extra weight for fresh articles. In this knowledge base entry you'll find example of a filter that gives double weight for posts within last week and half weight for older posts.

  21. jason_bassford
    Member
    Posted 11 months ago #

    Yes, I applied the solution. I just wanted to add another voice to those who use the plugin because of the content it finds, but who don't believe that sorting by relevance is always a prerequisite in order for the plugin to be useful. :)

  22. msaari
    Member
    Posted 11 months ago #

    Fine, fine, I give in - the option to change the default sort order is now available in Premium 1.5.6 and will be available in the free Relevanssi starting from version 2.9.1, which will be released shortly, I'm just waiting to confirm some other fixes.

  23. deadhippo
    Member
    Posted 11 months ago #

    Thanks for this. Sorting by date is perfect for my site too. You have to be careful though every time you change your setting as the dropdown reverts to "relevant".

  24. msaari
    Member
    Posted 11 months ago #

    It shouldn't, I'll check and fix that.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags