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.
What is the link to your site? lien caché
Hello there
Have you tried using wpv_filter_query to change the orderby parameter? For example:
Please add the following code to your theme's "functions.php" file:
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);
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.
More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
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?
Hello,
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.
Privacy and Security Policy
We have strict policies regarding privacy and access to your information. Please see:
https://toolset.com/purchase/support-policy/privacy-and-security-when-providing-debug-information-for-support/
**IMPORTANT**
- 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: lien caché
Please, let me know if you need any additional details. Have a nice day.
Hey there,
It looks like the credentials that you shared with us are not working on our end. We are receiving a ‘wrong username/password’ error message.
Can you please review that information and get back to us?
I’m marking your next message as private again in case you need to send us new confidential data.
Thank you, please let us know.
Hello there,
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.
Thank you, please let us know.
Mateus
Feel free to do so. Thank you for your help. If you need me to create a staging environment on this end, just let me know.
If you could create the staging site that'd be super helpful.
I'm enabling the private fields again for your next reply.
Thank you!
Hey there,
I checked this out but so far I couldn't find a fix for it.
I asked my colleagues to have a look at this issue in order to see if there's anything I'm missing.
I'll come back here as soon as one of my colleagues reply back to me (which it shouldn't take long).
Thanks,
Thank you. Good to know I am not losing it (any more than usual) 🙂
Hello there,
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:
<?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 checked on the front end and it seems to be working now, can you please test it?
Best,
Mateus
Thank you for the help. Amazing work. This kind of support is getting more rare in the world today. Much appreciated.