Forums › Forums › Search & Filter Pro › Order (group) results by taxonomy term by default
- This topic has 6 replies, 2 voices, and was last updated 6 years, 1 month ago by
Anonymous.
-
Anonymous(Private) March 15, 2019 at 1:14 pm #205262
Hi, I’m trying to get search results ordered by a CPT taxonomy term. I thought the Relevanssi (premium) might work for this, but it doesn’t appear to be influencing the result at all.
Something like:
function calder_tax_weight( $match ) {
if ( has_term( ‘for-sale’, ‘listing_status’, $match->doc ) ) {
$match->weight = $match->weight * 5;
}
return $match;
}
add_filter( ‘relevanssi_match’, ‘calder_tax_weight’ );Is there another way to do this with S&F that doesn’t involve a separate loops in the results template?
Trevor(Private) March 15, 2019 at 3:09 pm #205272Our plugin does not sort by taxonomy terms. The reason is that you can have multiple tag/category/taxonomy terms) on individual posts, and which term would we use to sort on? WordPress stores terms for tags/categories/taxonomies in arrays
A sort requires a single value field, so on some custom fields this can be done (but not on ones that can have multiple values). Be aware that if there is no value for a taxonomy/custom field the post will be omitted from the results entirely.
Anonymous(Private) March 15, 2019 at 3:17 pm #205274That makes sense. Can you provide a little more info on how Relevanssi is integrated into the S&F results? I’m going to troubleshoot a bit more, but will probably end up using a meta value.
-Is a search term required for Relevanssi filtered results?
-How do the respective indexes interact?Thanks
Trevor(Private) March 15, 2019 at 3:41 pm #205278What our form does is the text search first, and receives back an array of Post ID’s, sorted if requested. It then further filters this list per the other filters on the page. Only this text search would use Relevanssi, or the default WordPress search. Relevanssi has no effect on results where the form has no text search.
If you want to sort, you may be able to use this filter:
https://searchandfilter.com/documentation/action-filter-reference/#edit-query-arguments
Anonymous(Private) March 15, 2019 at 4:12 pm #205283Thanks for confirming. What I was seeing with that filter is that WP Query does not have a direct way to group or sort by tax term. Existing solution all use a loop in a template.
For this application, duplicating the loop worked fine:
<?php while ($query->have_posts()) { $query->the_post(); if ( get_post_type( get_the_ID() ) == 'business_listing' && has_term('for-sale', 'listing_status') ) { echo do_shortcode( '[pods name="business_listing" template="Listing Snippet" id="'.get_the_ID().'"]' ); } } ?> <?php while ($query->have_posts()) { $query->the_post(); if ( get_post_type( get_the_ID() ) == 'business_listing' && has_term('under-loi', 'listing_status')) { echo do_shortcode( '[pods name="business_listing" template="Listing Snippet" id="'.get_the_ID().'"]' ); } } ?>
-
AuthorPosts