Newest first always

  • James
    Participant
    # 5 months, 3 weeks ago

    I have a site that’s using wpsolr for search. When you use multiple search words: “one two three”
    You will get results newest first for everything that matches “one two three”, then it starts over for “two three one” or “One two” etc.

    Is there any way to always get chronological order for all the results returned from search?

    Thank you

    wpsolr
    Keymaster
    # 5 months, 3 weeks ago

    Is there any way to always get chronological order for all the results returned from search?

    You want to sort results by date?

    James
    Participant
    # 5 months, 3 weeks ago

    Yes, right now they are sorted by date and by relevancy (when you use multiple keywords).

    Can I force it to always return by date in the case when someone uses multiple keywords?

    Thanks

    wpsolr
    Keymaster
    # 5 months, 3 weeks ago

    You can set the default sort to be by date. But unconditionally.

    James
    Participant
    # 5 months, 3 weeks ago

    When you say unconditionally do you mean there is a setting that ignores relevancy when using sort by date (with multiple keywords)? If so, how would I set that? If there is a hook I can use I am willing to write code for it.

    Or did you mean I can set the sort by date but I can’t change the default behavior with multiple keywords?

    wpsolr
    Keymaster
    # 5 months, 3 weeks ago

    Both.

    Default sort is fixed, but there is a hook to update it from code.

    Extracted from file wp-content/plugins/wpsolr-pro/wpsolr/pro/extensions/geolocation/class-wpsolr-option-geolocation.php:

    add_filter( WPSOLR_Events::WPSOLR_FILTER_DEFAULT_SORT_FIELDS, [
       $this,
       'wpsolr_filter_default_sort_fields',
    ], 10, 1 );
    * Get geolocation default sort
    *
    * @param string $default_sort
    *
    * @param WPSOLR_Query $wpsolr_query
    *
    * @return string
    */
    public function wpsolr_filter_default_sort( $default_sort, WPSOLR_Query $wpsolr_query ) {
    
     if ( $this->get_is_geolocation( $wpsolr_query ) ) {
      // It's a geolocation search, use default geolocation default sort
      $geolocation_default_sort = WPSOLR_Service_Container::getOption()->get_option_geolocation_default_sort();
    
      if ( ! empty( $geolocation_default_sort ) ) {
       // Use default geolocation default sort
       return $geolocation_default_sort;
      }
     }
    
     // No default geolocation default sort, or not a geolocation search: use the general default sort.
     return $default_sort;
    }
    James
    Participant
    # 5 months, 3 weeks ago

    Awesome. Thanks for your help

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.