Skip Navigation

[Gelöst] Not all results returned when ordering by a field

This support ticket is created vor 5 Jahre, 4 Monate. 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.

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

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 1 Antwort, has 2 Stimmen.

Last updated by Waqar vor 5 Jahre, 4 Monate.

Assisted by: Waqar.

Author
Artikel
#1154102

I have some custom post types (events), most of which have a date in a custom field, but not all. Some of them are regular for example so a single date is not appropriate. When ordering by the date custom field in the view, I would expect any that don't have a date set to appear at the end, but they are not returned at all. Is this by design?

#1154872

Waqar
Supporter

Languages: Englisch (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Zoe,

Thank you for contacting us and I'll be happy to assist.

Your observation is correct and this behavior is due to a limitation in the WordPress query when it is set to order posts based on custom field values.
( ref: https://codex.wordpress.org/Class_Reference/WP_Meta_Query )

To overcome this and show all events, you can use the following steps:

1. First, please a create a view that shows events ordered by your date custom field value. At this point, events which are without a set date won't show.

2. Next, you can duplicate that view, but in this new view's "Ordering" section, select "Order by" field to "Post title".

3. To make this new view show only event posts without a set date field, you can add the following code in the active theme's "functions.php" file:


add_filter( 'wpv_filter_query', 'filter_date_custom_fn', 1000 , 3 );
function filter_date_custom_fn( $query_args, $view_settings ) {

	if ( (!is_admin() && isset($view_settings['view_id'])) && ($view_settings['view_id'] == 1234) ) {

	$query_args['meta_query'] = array(
								'relation' => 'OR',
								'date_field' => array(
									'key' => 'wpcf-date-field-slug',
									'compare' => 'NOT EXISTS',
									), 
								);
	}

	return $query_args;
}

Note: Please replace "1234" with the actual ID of this new view and "date-field-slug" with the actual slug of your date field.
(making sure that it starts with "wpcf-")

4. As a result, with help of these two views, you'll be able to show the complete list of events, by placing their shortcodes on top of each other.

I hope this helps and let me know if you have any questions related to this setup.

regards,
Waqar

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.