[Resolved] Secondary sort not working based on title
This thread is resolved. Here is a description of the problem and solution.
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:
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.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
Tell us what you are trying to do? Create a primary sort based on a custom field, a secondary sort based on the title (alpha)
Is there any documentation that you are following? NONE
Is there a similar example that we can see? No, no matter what I change on secondary, the primary sort seems to drive it all. I am not sure if its caching or another issue.
Please replace 1234 with your post view's ID, replace custom-field with the custom field slug.
Please notice that I'm passing the priority of 101 to the hook, it is important to pass a high value in order to guarantee its execution at the right moment.
I added the view id, added the slug for the custom field, and left it at 101. The secondary sort isn't working. The actual view still has the custom field at the sort as well. Does that need to be set something different?
I'd have to investigate it to see what's causing this issue.
I would like to request temporary access (wp-admin and FTP) to your site to take better look at the issue. You will find the needed fields for this below the comment area when you log in to leave your next reply. The information you will enter is private which means only you and I can see and have access to it.
Our Debugging Procedures
I will be checking various settings in the backend to see if the issue can be resolved. Although I won't be making changes that affect the live site, it is still good practice to backup the site before providing us access. In the event that we do need to debug the site further, I will duplicate the site and work in a separate, local development environment to avoid affecting the live site.
- Please make a backup of site files and database before providing us access.
- If you do not see the wp-admin/FTP fields this means your post & website login details will be made PUBLIC. DO NOT post your website details unless you see the required wp-admin/FTP fields. If you do not, please ask me to enable the private box. The private box looks like this: hidden link
Please, let me know if you need any additional details. Have a nice day.
Thank you for sharing that information, I was able to connect.
I'm still debugging it but I'd like to ask permission to make a copy/staging version of your site where I can debug this closely without affecting the live site. For example, I may need to disable plugins or themes, it is better to do it in a test site.
I'll make sure to delete this copy as soon as we get this issue fixed.
I'm afraid of debugging directly on the live site and cause issues to your visitors. Also, it is important to test a different setup as part of the troubleshooting.
I checked it and the issue happens specifically with the CPT Products, for other CPTs we're able to combine the primary and secondary sorting without any issues.
This has been already reported to our team and we'll work a definitive solution.
In the meantime we were able to find a workaround. I tested it on the staging site and it worked correctly, then I went ahead and replicated it in the live site. These are the steps:
Set up the primary sorting in the view
Add the following code to the theme's functions.php file to create the secondary sorting by title: