Custom sorting for archive pages
- jnearsParticipant11 months, 1 week 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
jnearsParticipant11 months ago #36677I’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
wpsolrKeymaster11 months ago #36678This 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; }
jnearsParticipant11 months ago #36679This 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 11 months ago by jnears.
wpsolrKeymaster11 months ago #36694Thank 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!
jnearsParticipant11 months ago #36695Hi
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;
}
You must be logged in to reply to this topic.