• Hi Mikko, I use the free version of Relevanssi. In the site editor, I have added the default search block as well as the post-loop block in the search template. I also embedded the following code in functions.php:

    add_action( 'pre_get_posts', 'wpshout_pages_blogindex' );
    function wpshout_pages_blogindex( $query ) {
    if ( is_search() && $query->is_main_query() ) {
    $query->set( 'post_type', 'post' );
    $query->set( 'orderby', 'date' );
    $query->set( 'order', 'DESC' );
    }
    }


    When I have Relevanssi disabled, the newest or last modified posts are displayed at the top. But when I enable Relevanssi, the newest posts are no longer displayed. I have tried many Relevanssi filters, but unfortunately I just can’t get the most recently modified posts to show at the top. Can you help me?

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

    (@msaari)

    Your function looks great and works perfectly on my test site.

    If you run the query with the Relevanssi admin search (Dashboard > Admin search), what’s the order of results there?

    Is it possible something else is overriding the order of the results? You can use the relevanssi_orderby filter hook to see the orderby parameter Relevanssi is getting – is it still date there or has it been changed to something else?

    Thread Starter raddero

    (@raddero)

    The order of results in admin search is the same like in the frontend. Is it possible that posts with status “publish” are prioritized before post with status “private”? It’s strange: when I create a new post and set it to “private”, the post should be displayed at the top. But other posts are displayed before it. All users have the right to see private posts. So it is not because of the right assignment.

    Here are some values of the admin search:

    Query Variables

    • posts_per_page: 100
    • post_types: post
    • relevanssi_admin_search: 1
    • orderby: date
    • order: asc
    • relevance: desc
    • operator: OR

    Filter

    relevanssi_search_ok

    • 10: relevanssi_block_on_admin_searches
    • 10: relevanssi_acf_relationship_fields
    • 11: relevanssi_control_media_queries

    relevanssi_modify_wp_query

    • 10: rlv_asc_date

    relevanssi_where

    • 1: relevanssi_post_date_throttle_where

    relevanssi_join

    • 1: relevanssi_post_date_throttle_join

    relevanssi_query_filter

    • 10: relevanssi_limit_filter

    relevanssi_post_ok

    • 9: relevanssi_default_post_ok
    Plugin Author Mikko Saari

    (@msaari)

    Post status has no effect on the post weight. It only affects who sees the post.

    What does the relevanssi_orderby filter show you? Can I see your site? If that’s possible, please enable the debugging mode from the Relevanssi debugging settings.

    Thread Starter raddero

    (@raddero)

    Thank you very much for your reply. The relevanssi_orderby filter shows me all private and published posts, but not the latest first. Even if I create a whole new post, it is not shown at the top (within the post loop block). I’d be happy to let you look at it on my website. Can I send you the link privately e.g. by email?

    Plugin Author Mikko Saari

    (@msaari)

    The relevanssi_orderby filter should not show any posts; it should show the value of the orderby parameter. Does it?

    Soliciting contact outside the forums is against the forum rules; it’s also a service I only provide to paying customers.

    Thread Starter raddero

    (@raddero)

    Yes, it shows a parameter when I enfold the filter in the admin search:

    Filter

    relevanssi_orderby

    • 10: rel_orderby

    Did you mean that?

    Plugin Author Mikko Saari

    (@msaari)

    I mean this:

    add_filter( 'relevanssi_orderby', function( $orderby ) {
      var_dump($orderby);
      return $orderby;
    } );

    This will print out the actual orderby value Relevanssi uses.

    Thread Starter raddero

    (@raddero)

    Yes, it displays following lines:

    array(1) { ["orderby"]=> string(9) "post_date" }

    Plugin Author Mikko Saari

    (@msaari)

    What if you look at the posts at the relevanssi_hits_filter?

    add_filter( 'relevanssi_hits_filter', function( $hits ) {
      foreach ( $hits[0] as $hit ) {
        echo "$hit->post_date: $hit->post_title<br />";
      }
      return $hits;
    } );

    Are the posts in the right order here?

    Thread Starter raddero

    (@raddero)

    No, thats not the right order and I miss some posts (= latest posts). It shows a lot of posts, but not all and not the latest.

    Plugin Author Mikko Saari

    (@msaari)

    That’s weird. The relevanssi_hits_filter hook happens pretty much immediately after the posts are sorted, so if the orderby is correct – and we’ve checked it is – the posts should be in the correct order. I can’t really say much more without seeing the site.

    How many posts are there on your site? Is the throttle enabled in Relevanssi searching settings? If the search returns more than 500 posts, the throttle if enabled will cut off some of the posts. That may explain some missing posts.

    Thread Starter raddero

    (@raddero)

    Yes, there are more than 500 posts. I deleted some posts (on environment) and now the last post in the hit list is the latest post. But how can I have this post at the top?

    Thread Starter raddero

    (@raddero)

    I believe that I have found the “error”. It is probably due to the throttling. I deleted 600 posts on my development page so I only have 400 posts. I have disabled the throttling. Now the newest post is displayed at the top. But when I have over 500 posts, the newest post is no longer displayed at the top.

    I use the remove_filter( 'relevanssi_query_filter', 'relevanssi_limit_filter' ); and now the latest post is visible at the top.

    • This reply was modified 2 years, 9 months ago by raddero.
    Plugin Author Mikko Saari

    (@msaari)

    Yeah, throttling and the date order don’t combine well because throttling is based on post relevance, not freshness, so it can remove the latest posts.

    The best solution is indeed to remove the throttle.

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Orderby Latest Posts’ is closed to new replies.