Skip Navigation

[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:

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

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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 10 replies, has 2 voices.

Last updated by Mateus Getulio 4 months, 3 weeks ago.

Assisted by: Mateus Getulio.

Author
Posts
#2704882

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? hidden link

#2704901

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

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

#2705030

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?

#2705036

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

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: hidden link

Please, let me know if you need any additional details. Have a nice day.

#2705041

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

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.

#2705177

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

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

#2705565

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.

#2705584

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

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!

#2705830

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

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,

#2705831

Thank you. Good to know I am not losing it (any more than usual) 🙂

#2705943

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

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

#2705947

Thank you for the help. Amazing work. This kind of support is getting more rare in the world today. Much appreciated.