Support » Plugin: Relevanssi - A Better Search » Woocomerce sort by date problem.

  • Resolved JapeNZ

    (@japenz)


    Hi there,
    I’m just testing your plugin and can’t get the sort by date option to work.
    I have it set to sort by relevance as default, but the search results page has a drop down take allows customers to also select sort by date, but it’s not displaying the products by the date they were added new to old (preferable) or old to new.

    Any idea what might be the problem?

    Changing the search option to date (Search by latest) does change the results order, but it doesn’t seem to be by the date the products were posted?

    Thank you for your help!

    Kind regards,
    JP

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

    (@msaari)

    If the dropdown is setting the orderby parameter to post_date and that is not changing the order of the results, something else may override the orderby parameter elsewhere.

    If you add this snippet to your site (theme functions.php, for example), does that sort the posts correctly?

    add_filter( 'relevanssi_modify_wp_query', function( $query ) {
      $query->set( 'orderby', 'post_date' );
      $query->set( 'order', 'desc' );
      return $query;
    } );
    Thread Starter JapeNZ

    (@japenz)

    Hi @msaari
    Thank you for taking a look at this for me!

    Your snippet does make the search result order by date, but it also overrides the other sort options.
    If I have the snippet active, selecting the other options such as relevance or alphabetical gives the same ‘post_date’ result.

    Any thoughts on how I can target or replace the existing orderby post date for the one in your snippet, without effecting the other orderby options?

    Thanks again!

    Kind regards,
    JP

    Plugin Author Mikko Saari

    (@msaari)

    Yes, this snippet should override anything else. It was just to check that the date sorting is working.

    If you choose date sorting from your search results page, does it set the orderby parameter to post_date or something else? Can I see your site?

    Thread Starter JapeNZ

    (@japenz)

    Ah right okay, good to know it works 😀

    My website is: https://comicbookshop.co.nz/

    If you search ‘cgc’ you can see the results as they currently are, then let me know and I’ll add your snippet so you can see how the orderby date should look.

    If that’s okay.

    Kind regards,
    JP

    Plugin Author Mikko Saari

    (@msaari)

    When I set the order to date, I get this URL:

    https://comicbookshop.co.nz/?orderby=date&paged=1&s=cgc&post_type=product

    This, I assume, is the right order:

    https://comicbookshop.co.nz/?orderby=post_date&paged=1&s=cgc&post_type=product

    You can try this:

    add_filter( 'relevanssi_modify_wp_query', function( $query ) {
      if ( 'date' === $query->get( 'orderby' ) ) {
        $query->set( 'orderby', 'post_date' );
      }
      return $query;
    } );
    Thread Starter JapeNZ

    (@japenz)

    The new snippet posts the date back in reverse order for some reason.
    I assume I should add asc or desc somewhere?

    Plugin Author Mikko Saari

    (@msaari)

    add_filter( 'relevanssi_modify_wp_query', function( $query ) {
      if ( 'date' === $query->get( 'orderby' ) ) {
        $query->set( 'orderby', 'post_date' );
        $query->set( 'order', 'desc' );
      }
      return $query;
    } );
    Thread Starter JapeNZ

    (@japenz)

    Oh damn it, sorry I used your link rather than the snippet to check… doh!

    Snippet isn’t doing anything unfortunately.

    Plugin Author Mikko Saari

    (@msaari)

    That’s weird. The filter works on my test site. Also, it shouldn’t even be necessary – Relevanssi should convert date to post_date without extra help.

    What’s in the $query->query_vars? You can find debugging instructions here: https://www.relevanssi.com/knowledge-base/debugging-relevanssi-searching-issues/#look-at-the-parameters-relevanssi-is-getting.

    Thread Starter JapeNZ

    (@japenz)

    Yeah, I figured it was strange! xD

    Unfortunately, new stock got delivered and I had to go take care of that… sorry for not following up sooner.

    Is this what you mean?

    array(71) { ["s"]=> string(3) "cgc" ["post_type"]=> array(2) { [0]=> string(7) "product" [1]=> string(17) "product_variation" } ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(0) "" ["pagename"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(0) "" ["tag"]=> string(0) "" ["cat"]=> string(0) "" ["tag_id"]=> string(0) "" ["author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["meta_key"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["sentence"]=> string(0) "" ["title"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["embed"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(2) { [0]=> string(6) "132201" [1]=> string(6) "132216" } ["post_name__in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in"]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["orderby"]=> string(9) "relevance" ["order"]=> string(4) "DESC" ["meta_query"]=> array(0) { } ["tax_query"]=> array(3) { [0]=> array(4) { ["taxonomy"]=> string(18) "product_visibility" ["field"]=> string(4) "slug" ["terms"]=> array(1) { [0]=> string(10) "outofstock" } ["operator"]=> string(6) "NOT IN" } [1]=> array(4) { ["taxonomy"]=> string(18) "product_visibility" ["field"]=> string(4) "slug" ["terms"]=> array(1) { [0]=> string(19) "exclude-from-search" } ["operator"]=> string(6) "NOT IN" } ["relation"]=> string(3) "AND" } ["wc_query"]=> string(13) "product_query" ["posts_per_page"]=> int(20) ["iconic_ssv_query"]=> int(1) ["ignore_sticky_posts"]=> bool(false) ["suppress_filters"]=> bool(false) ["cache_results"]=> bool(true) ["update_post_term_cache"]=> bool(true) ["lazy_load_term_meta"]=> bool(true) ["update_post_meta_cache"]=> bool(true) ["nopaging"]=> bool(false) ["comments_per_page"]=> string(2) "50" ["no_found_rows"]=> bool(false) ["search_terms_count"]=> int(1) ["search_terms"]=> array(1) { [0]=> string(3) "cgc" } ["search_orderby_title"]=> array(1) { [0]=> string(173)

    Plugin Author Mikko Saari

    (@msaari)

    Yes, that’s it. It shows the orderby is set to relevance – is this from the default value? What if you set the order to post date from the dropdown?

    Thread Starter JapeNZ

    (@japenz)

    Okay, so this one?

    array(71) { ["s"]=> string(3) "cgc" ["paged"]=> int(1) ["orderby"]=> string(7) "date ID" ["post_type"]=> array(2) { [0]=> string(7) "product" [1]=> string(17) "product_variation" } ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(0) "" ["pagename"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(0) "" ["tag"]=> string(0) "" ["cat"]=> string(0) "" ["tag_id"]=> string(0) "" ["author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["meta_key"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["sentence"]=> string(0) "" ["title"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["embed"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(2) { [0]=> string(6) "132201" [1]=> string(6) "132216" } ["post_name__in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in"]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["order"]=> string(4) "DESC" ["meta_query"]=> array(0) { } ["tax_query"]=> array(3) { [0]=> array(4) { ["taxonomy"]=> string(18) "product_visibility" ["field"]=> string(4) "slug" ["terms"]=> array(1) { [0]=> string(10) "outofstock" } ["operator"]=> string(6) "NOT IN" } [1]=> array(4) { ["taxonomy"]=> string(18) "product_visibility" ["field"]=> string(4) "slug" ["terms"]=> array(1) { [0]=> string(19) "exclude-from-search" } ["operator"]=> string(6) "NOT IN" } ["relation"]=> string(3) "AND" } ["wc_query"]=> string(13) "product_query" ["posts_per_page"]=> int(20) ["iconic_ssv_query"]=> int(1) ["ignore_sticky_posts"]=> bool(false) ["suppress_filters"]=> bool(false) ["cache_results"]=> bool(true) ["update_post_term_cache"]=> bool(true) ["lazy_load_term_meta"]=> bool(true) ["update_post_meta_cache"]=> bool(true) ["nopaging"]=> bool(false) ["comments_per_page"]=> string(2) "50" ["no_found_rows"]=> bool(false) ["search_terms_count"]=> int(1) ["search_terms"]=> array(1) { [0]=> string(3) "cgc" } ["search_orderby_title"]=> array(1) { [0]=> string(173)

    This is the same with or without your snippet applied.

    • This reply was modified 2 months, 2 weeks ago by JapeNZ.
    Plugin Author Mikko Saari

    (@msaari)

    Ok, that’s the problem. The orderby value isn’t date, it’s date ID. That’s weird.

    This should help:

    add_filter( 'relevanssi_modify_wp_query', function( $query ) {
      if ( 'date ID' === $query->get( 'orderby' ) ) {
        $query->set( 'orderby', 'post_date' );
        $query->set( 'order', 'desc' );
      }
      return $query;
    } );
    Thread Starter JapeNZ

    (@japenz)

    Perfect, nailed it!

    Thank you so much for all your help 🙂

Viewing 14 replies - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.