Custom sorting for archive pages

  • jnears
    Participant
    6 months, 4 weeks ago #36056

    Hi

    I have configured a few different search landing pages using custom post-type archive pages.

    The initial sort on the majority of the pages is correct (sorted by title, which I configured in the WPSOLR module).

    On one of the archive pages, I need the initial sort to use an advanced custom field; however, I cannot see a way of achieving this. The default sort in the WPSOLR module only allows for a default sort that applies across the site.

    Please advise.

    Many thanks

    Jonathan

    wpsolr
    Keymaster
    6 months, 4 weeks ago #36097

    You can configure views with different settings.

    – Screen 2.1: “default” view replaces the search archive. New view replaces the category archive.
    – Screen 2.5 Sort: each view with its own default sort.

    jnears
    Participant
    6 months, 3 weeks ago #36677

    I’m not sure this is granular enough for my needs?

    I have a custom post type called ‘Publication’ with an archive page that uses WPSOLR. I need this archive default sort by a custom field (publication date).

    I have a custom post type called ‘Variable’ with an archive page that uses WPSOLR. I need this archive default sort to be alphabetical.

    WPSOLR allow the creation of views on pre-existing WordPress search or categories; however, it does not seem to allow views to be created on individual archive pages? I realise there is a check box to replace archives with WPSOLR results, but this doesn’t allow the granularity of targeting each archive differently?

    Apologies if I’m incorrect on this.

    If there is no way to set the sort order on individual archives, can I use code to intercept the Solr call somehow?

    Thanks

    wpsolr
    Keymaster
    6 months, 3 weeks ago #36678

    This is correct. In your use case, you will need some code.

    use wpsolr\core\classes\WPSOLR_Events;
    add_filter( WPSOLR_Events::WPSOLR_FILTER_DEFAULT_SORT, 'wpsolr_filter_default_sort', 10, 2 );
    public function wpsolr_filter_default_sort( $default_sort, $wpsolr_query ) {
      if ( /* Your condition here*/ ) {
        return $my_sort;
      }
      return $default_sort;
    }
    jnears
    Participant
    6 months, 3 weeks ago #36679

    This makes some sense, but could you clarify if something like the below would work targeting a custom post-type archive of ‘publications’ and a sort I created within the WPSOLR admin called ‘publications_desc’

    I’m not sure where/how the $my_sort in your example would be created.

    use wpsolr\core\classes\WPSOLR_Events;
    add_filter( WPSOLR_Events::WPSOLR_FILTER_DEFAULT_SORT, 'wpsolr_filter_default_sort', 10, 2 );
    function wpsolr_filter_default_sort( $default_sort, $wpsolr_query ) {
      if ( is_post_type_archive( 'publication' ) ) {
        return $publications_desc_sort;
      }
      return $default_sort;
    }
    • This reply was modified 6 months, 3 weeks ago by jnears.
    wpsolr
    Keymaster
    6 months, 3 weeks ago #36681

    It looks good. Just use the field name that you see in your urls when you use your sort.

    return 'sort_by_title_s_asc';

    return 'sort_by_date_desc';

    return 'sort_by_last_modified_desc';

     

    Image wpsolr-sort-ids.png of

    jnears
    Participant
    6 months, 3 weeks ago #36688

    Great
    Thanks so much for the quick reply
    Jonathan

    wpsolr
    Keymaster
    6 months, 3 weeks ago #36694

    Thank you!

    If you’re satisfied with our service, I invite you to share your use case in our LinkedIn group.

    Your contribution will be greatly valued by fellow users!

    jnears
    Participant
    6 months, 3 weeks ago #36695

    Hi

    This code does work, but I cannot use the ‘public’ keyword for the function. I’m not sure if this matters? If I add it, I get the following error: ‘Parse error: syntax error, unexpected token “public” ….’

    Without the public keyword, the function is working; however, I’m getting the following message on the archive page: ‘Warning: Undefined variable $pubs_publication_date_str_asc’

    I have tried activating this sort in the ‘Activate/deactivate items in the sort list’ but still get the error.

    The error shows at the top of the page and where the widget filters sit.

    My code:

    use wpsolr\core\classes\WPSOLR_Events;
    add_filter( WPSOLR_Events::WPSOLR_FILTER_DEFAULT_SORT, ‘wpsolr_filter_default_sort’, 10, 2 );
    function wpsolr_filter_default_sort( $default_sort, $wpsolr_query ) {
    if ( is_post_type_archive( ‘publication’ ) ) {
    return $pubs_publication_date_str_asc;
    }
    return $default_sort;
    }

    wpsolr
    Keymaster
    6 months, 3 weeks ago #36696

    ‘public’ is not necessary for a function indeed, it was used in my code for a class method.

    Use string constant 'pubs_publication_date_str_asc' instead of $pubs_publication_date_str_asc

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

You must be logged in to reply to this topic.