Skip Navigation

Sorting a view by a parameter based on a custom field value

This support ticket is created Il y a 9 années et 10 mois. There's a good chance that you are reading advice that it now obsolete.

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.

This topic contains 4 réponses, has 2 voix.

Last updated by Juan Il y a 9 années et 10 mois.

Assigned support staff: Caridad.

Auteur
Publications
#28603

Juan
Supporter

Timezone: Europe/Madrid (GMT+02:00)

Hi.

I'm trying to do a quite complex thing here.

I have two custom post types, one parent of the other. I've created a View to list my children ones on their parent page, and it works like a charm.

I've ordered this children by a custom field, but I need to order them by a parameter based on this custom field value instead of by the actual custom field vaue. Actually this is not supported by the WP_Query orderby parameter, but it's doable using a custom MySQL query and I'm quite sure there is a filter inside Views that could allow me to do it.

I've looked at the Views plugin code, and if I understand correctly the main query filter used to show this view is on embedded/inc/wpv-filter-query.php on the function wpv_filter_get_posts. On line 50, there is a filter to this WP_Query arguments used to reorder it and named wpv_filter_query.

This filtering is done on embedded/inc/wpv-filter-order-by-embedded.php on line 17 and through the function wpv_filter_get_order_arg. This function, on line 95, does the actual reordering by another filter to post_orderby using the function wpv_post_order_by_meta.

This wpv_post_order_by_meta function gets all the desired posts with the desired custom field and reorders them using the meta_value in a MySQL statement, returning a valid value to the post_orderby. filter

I've tried adding a filter to the post_orderby since I can't do a normal WP_Query modification, with no luck at all. I've seen no other filter available to modify any MySQL statement related to reordering.

I would appreciate any advice or correction. I know I have to write a filter hooked into Views routines, I have no problem with it. I just can't point to where it is.

Thanks in advance.

#28741

Dear Depaco,

Since WP_Query doesn't support that parameter, neither will Views. Your idea of using the posts_orderby filter is correct, but watch out for the extra 's', its posts_orderby. If you see its not working you might have to give it higher priority.

Please let me know if there is anything else that I can assist you with.

Regards,
Caridad

#28751

Juan
Supporter

Timezone: Europe/Madrid (GMT+02:00)

Hi Caridad.

Thanks for your answer.

I gave it a quite high priority (tried even with 2000) with no luck: my filter just adds one little change - CHAR_LENGTH({$order_by_value}) instead of {$order_by_value} - to the MySQL statement and it seems not to be fired at all. In fact, I kind of hacked this filter on the original plugin code on embedded/inc/wpv-filter-order-by-embedded.php on line 128 and it seems not to be doing anything. Even setting by hand the order to ASC or DESC instead of {$orderby_meta['order']} doesn't change the output..

I checked the extra 's' on the 'posts_orderby' filter, and I think I found an error on embedded/inc/wpv-filter-order-by-embedded.php on line 138: this extra 's' is missing there.

I wrote a workaround to store this custom field string length on another custom field created outside of Types. After adding this field to Custom Fields Control inside Types and making it numeric, I can set it as the sorting order for my View, but behaves as if it were a text field - i.e. 4>11 -, so there is a bug here too.

Hope it helps. I'm not sure if this is solvable, but any extra advice would be appreciated.

Regards,
Juan.

#28762

Dear Depaco,

This sort of code works fine here in my tests:

add_filter('posts_orderby', 'my_client_order', 10, 2);
function my_client_order($order, $q) {
       return 'CHAR_LENGTH(post_title) ASC';
}

About the missing 's', I have notified the developers about it.

Please let me know if there is anything else that I can assist you with.

Regards,
Caridad

#28777

Juan
Supporter

Timezone: Europe/Madrid (GMT+02:00)

Hi Caridad.

I see where my problem was: I was relying on global $orderby_meta, as seen on wpv_post_order_by_meta, but it renders as an empty array. I needed to reorder an specific query so I checked against that $query query_vars instead and everything works fine now.

Thanks a lot.

Regards,
Juan de Paco.