Skip Navigation

[Resolved] Secondary Sorting by Custom Field

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 6 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

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 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 33 replies, has 2 voices.

Last updated by keithT-3 6 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#627896

Sorry, it does not work. I added your code as-is to the functions.php and it did not change the order. It still is sorting by the city only on this page - hidden link.

#628364

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Keith,

This is because on most of the posts that I checked their shipping priority is set to 10.

So the only thing that it could be sorted by is the city.

Thanks,
Shane

#628403

Shane, please confirm. You're saying now that we can only sort by one custom field? Is that because I am using Advanced Custom Fields, or would that same limitation be true even if I was using WP Types Custom Fields?

#628418

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Keith,

No i'm not saying that. It is possible to sort by 2 custom fields.

The reason why its not working is because I checked on the posts and there was no field called acf_directory_sort_order, only a city field.

The other field that I saw with any ordering was the shipping priority field but this field from what I see most post have 10 as the value.

Thanks,
Shane

#628428

Okay, then I guess I am not clear when you there was no field with name acf_directory_sort_order. It is on the Provider custom post type as you can see here - hidden link. And it can be seen here ACF Field Group called "Directory Listing Information" - hidden link.

So, where is it you are seeing the field? It was added later, so maybe some records don't have it recorded in the database record, but the field is definitely there.

#628519

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Keith,

Here is another revision of the code.

This one i've tested to ensure the values change when i'm trying to sort.


add_filter('wpv_filter_query', 'add_custom_fields_to_view_query', 99, 3);
function add_custom_fields_to_view_query($query_args, $view_settings, $view_id ) {
        if ( $view_id == 16257 ){
        
 
       $meta_query = array();
       $meta_query[] = array('key' => 'acf_directory_sort_order');
       $meta_query[] = array('key' => 'acf_shipping_city');
       if ( !isset($query_args['meta_query']) ){
            $query_args['meta_query'] = array();
       }
       $query_args['meta_query'] = array_merge($meta_query, $query_args['meta_query']);
       add_filter('posts_orderby', 'custom_order');
   }
 
   return $query_args;
}
function custom_order($orderby) {
   global $wpdb;
   return $wpdb->postmeta.'.meta_value ASC, mt1.meta_value ASC';
}

Please let me know if this helps.
Thanks,
Shane

#628560

So, why would it be working on your test site and not on my test site? I've added the code to functions.php in the child theme. Then I went to a state with a smaller sampling than Florida, in this case Nevada - hidden link. And I made sure to update something on each of those records so for sure the custom order field would be saved in the database. And I changed the first record on the page to have a sort value of 200, so it should go to bottom since all others have a sort value of 100. But the record did not go to the bottom.

Am I missing something?

#628562

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

I'm logging in to check whats happening here.

#628563

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Keith,

Could you check the sort order here hidden link

Also this function here you can change the sort order .

function custom_order($orderby) {
   global $wpdb;
   return $wpdb->postmeta.'.meta_value ASC, mt1.meta_value ASC';
}

You can change it to ASC or DESC

#628565

Interesting. I do now see it is working on that page - hidden link. That page is not actually one I use because I don't want to present ALL the providers. So, hence I am using this URL_PARAM that is grabbing a smaller group (in this case a single state) - hidden link. That is where it is not working, but where I need it to work.

We are so close, and I really appreciate your help on this.

#629810

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Keith,

I'm taking another look at this for.

Hopefully we can resolve this one soon.

Thanks,
Shane

#629822

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Could you change the ID that the code should target to 16253 and let me know if it starts working now.

#629832

This is very strange. I went to the dev site sample page to test and it actually sorted the way I wanted. I tried two different states
hidden link
hidden link

And that was without changing the ID. If I changed the ID then the order seems totally random. So, then I copied the code onto the functions.php of the live site. But it did not work. Did I need to do something in addition to the just the code on functions.php? Even after clearing cache it didn't work.

Feels like we are so close.

#629847

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Keith,

Just to ensure that we are on the same page .

This is the view that we should be targetting correct?
hidden link

#629850

No, that is the incorrect one. This is the one -
hidden link

The forum ‘Types Community Support’ is closed to new topics and replies.