|
Secondary sort not working based on title
Started by: IntegrisDesign
in: Toolset Professional Support
Quick solution available
Problem:
The customer wants to create a primary sort based on a custom field and a secondary sort based on the title (alphabetical). Despite trying various changes, the primary sort seems to override the secondary sort. The customer is unsure if the issue is related to caching or another problem.
Solution:
I initially suggested using the wpv_filter_query to modify the orderby parameter in the theme's functions.php file. This involved adding the following code, replacing '1234' with the post view's ID and 'custom-field' with the custom field slug:
add_filter('wpv_filter_query', function($query, $setting, $view_id){
if(in_array($view_id, array(1234))){
$query['meta_key'] = 'wpcf-' . 'custom-field';
$query['orderby'] = array('title' => 'ASC', 'meta_value_num' => 'ASC');
}
return $query;
}, 101, 3);
The customer reported that the secondary sort was still not working. After further investigation and testing with a staging site, I identified that the issue was specific to the CPT Products.
I provided a workaround that involved modifying the orderby filter to include secondary sorting by title. The following code was added to the theme's functions.php file:
<?php
function func_orderby_title($orderby) {
global $WP_Views;
if ($WP_Views->current_view == 14670) {
$orderby = $orderby . ", wp_posts.post_title ASC";
}
return $orderby;
}
add_filter('posts_orderby', 'func_orderby_title');
?>
I tested the solution on the staging site and then replicated it on the live site, confirming that the sorting issue was resolved.
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
|
|
2 |
15 |
3 weeks, 2 days ago
Mateus Getulio
|
|
Simple question Rank Math plugin integration 2024
Started by: hernanL
in: Toolset Professional Support
Quick solution available
|
|
2 |
5 |
3 months ago
Mateus Getulio
|