Skip Navigation

[Resolved] Display upcoming events order by custom field

This support ticket is created 5 years, 6 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by marianD-3 5 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#1250143

Hi

I have a list of events and I'd like to filter these events by date. The date field comes from a custom field ([rhc_next_upcoming_dates]) ( hidden link ) which is not built with Toolset.

I have tried this but it doesn't work

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 == 61 ){
return;
}

$meta_query = array();
$meta_query[] = array('key' => 'rhc_next_upcoming_dates');

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

Regards

#1250287
if ( $view_id == 61 ){
return;
}

This code basically says you want to apply the custom ordering filter to every View except View 61. Is that correct? If you only want to apply to view 61, then it should be this:

if ( $view_id != 61 ){
return;
}
$meta_query = array();
$meta_query[] = array('key' => 'rhc_next_upcoming_dates');

This code has nothing to do with Toolset, it's directly related to a WordPress Query using the 3rd-party custom field. If you need assistance, I suggest you reach out to the developers who created this rhc_next_upcoming_dates field to determine how to properly modify a WordPress Query to sort by their custom field value. Dates can be stored in many formats, so it's probably best to get their feedback. I'm glad to provide anything you need from Toolset to help facilitate this conversation. Our API's documentation for this filter is available here:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Here's another ticket that includes a code sample for ordering by a custom numeric value. Your 3rd-party system may be able to use this example to help you formulate the correct query manipulations: https://toolset.com/forums/topic/sorting-by-custom-field/

return $query_args;
};

This part is perfect, once you have made adjustments to the query in $query_args, you must return $query_args to the filter.

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

Not sure about this, again it's related to a 3rd-party system I don't know anything about.

#1254073

My issue is resolved now. Thank you!