Skip Navigation

[Closed] Sorting by custom field not working after WP4.2 upgrade

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 9 years 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
- 10:00 – 19:00 10:00 – 19:00 10:00 – 19:00 10:00 – 19:00 10:00 – 19:00 -
- - - - - - -

Supporter timezone: Europe/Madrid (GMT+01:00)

This topic contains 3 replies, has 3 voices.

Last updated by Caridad 8 years, 11 months ago.

Assisted by: Caridad.

Author
Posts
#299399

I am trying to: sort posts based on a custom field

I visited this URL: hidden link

I expected to see: hidden link

Instead, I got: Since we upgraded from WordPress 4.1 to WordPress 4.2 the sorting by custom fields doesn't seem to be working. The test site above is running 4.1.4 and the live site is running 4.2.1. They both have the same theme installed.

This is the code that is being used to retrieve the custom posts to display on the page, which are supposed to be displayed in reverse date order:

			$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
			$args = array(
				'post_type' => 'condo',
				'post_status' => 'publish',
				'posts_per_page' => '12', // you may edit this number
				'paged'					=> $paged,
				'meta_query'		=> array ( array ('key' => 'wpcf-listing-status', 'value' => '3', 'compare' => '='),),
				'orderby' 			=> array( 'meta_value_num' => 'DESC', 'date' => 'DESC' ), 
				'meta_key' 			=> 'wpcf-sold-date'
			);

			$building_items = null;
			$building_items = new WP_Query($args);

			// Pagination fix
			$temp_query = $wp_query;
			$wp_query   = NULL;
			$wp_query   = $building_items;

			if( $building_items->have_posts() ) :
				while ($building_items->have_posts()) : 
					$building_items->the_post(); 
                                        ...

On the 4.2 site the order seems to be totally random, whereas on the 4.1.4 site the order is working as it should.

How do I get the order working on the 4.2 site again?

Thank you.

#299649

Dear MikeS,

Your issue is not related to Toolset. It's purely WP issue. The reason for that is that WordPress core has been updated in in version 4.2 resulting in change in “order by?? and “meta query?? query classes. You have to update your code in order to make it work.

Please follow documentation and example at: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

in order to update your code.

Regards,
Peter

#299786

Hi,

I have updated my query, but it still isn't working, could you please help get it working.

Here is the updated query:

			$args = array(
				'post_type' => 'condo',
				'post_status' => 'publish',
				'posts_per_page' => '12', // you may edit this number
				'paged'					=> $paged,
				'meta_query'		=> array ( 
														'listing_clause' =>	array (
																'key' => 'wpcf-listing-status', 
																'value' => '3', 
																'compare' => '='
															),
													),
				'orderby' 			=> array( 
														'meta_value_num' => 'DESC', 
														'date' => 'DESC' 
													), 
				'meta_key' 			=> 'wpcf-sold-date',
				'meta_type'			=> 'DATE'
			);

Here is what it shows as the Select that this produces:

SELECT SQL_CALC_FOUND_ROWS ms15_posts.ID 
FROM ms15_posts 
INNER JOIN ms15_postmeta ON ( ms15_posts.ID = ms15_postmeta.post_id ) 
INNER JOIN ms15_postmeta AS mt1 ON ( ms15_posts.ID = mt1.post_id ) 
WHERE 1=1 AND ( ms15_postmeta.meta_key = 'wpcf-sold-date' AND ( ( mt1.meta_key = 'wpcf-listing-status' AND CAST(mt1.meta_value AS CHAR) = '3' ) ) ) AND ms15_posts.post_type = 'condo' AND ((ms15_posts.post_status = 'publish')) 
GROUP BY ms15_posts.ID 
ORDER BY ms15_posts.menu_order, ms15_postmeta.meta_value+0 DESC, ms15_posts.post_date DESC LIMIT 0, 12

which has the wpcf-sold-date in the WHERE clause instead of in the ORDER By clause.

So, I then tried:

			$args = array(
				'post_type' => 'condo',
				'post_status' => 'publish',
				'posts_per_page' => '12', // you may edit this number
				'paged'					=> $paged,
				'meta_query'		=> array ( 
														'listing_clause' =>	array (
																'key' => 'wpcf-listing-status', 
																'value' => '3', 
																'compare' => '='
															),
													),
				'orderby' 			=> array( 
														array (
															'meta_value_num' => 'DESC', 
															'meta_key' 			=> 'wpcf-sold-date',
															'meta_type'			=> 'DATE'
														),
														'date' => 'DESC' 
													), 				
			);

and got this SELECT:

SELECT SQL_CALC_FOUND_ROWS ms15_posts.ID 
FROM ms15_posts 
INNER JOIN ms15_postmeta ON ( ms15_posts.ID = ms15_postmeta.post_id ) 
WHERE 1=1 AND ( ( ms15_postmeta.meta_key = 'wpcf-listing-status' AND CAST(ms15_postmeta.meta_value AS CHAR) = '3' ) ) AND ms15_posts.post_type = 'condo' AND ((ms15_posts.post_status = 'publish')) 
GROUP BY ms15_posts.ID 
ORDER BY ms15_posts.menu_order, ms15_posts.post_date DESC LIMIT 0, 12

which totally lost the ordering by wpcf-sold-date.

Any ideas how to get the $arg so it works like it used to in 4.1?

Thank you.

#307628

You need to include a reference to the date to be able to order by it.

You can read about here as Peter said, pay special attention to the "handles" part:
https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

The topic ‘[Closed] Sorting by custom field not working after WP4.2 upgrade’ is closed to new replies.