Ordering wp_query posts from custom field not working
-
I have created a custom field which store in wp_postmeta. This custom field hold a number which I want to use for ordering.
I was looking at the codex to do this: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
Here’s my try at this:
$args = array(
‘post_type’ => ‘download’,
‘meta_key’ => ‘_custom_order’,
‘orderby’ => ‘meta_value_num’,
‘order’ => ‘DESC’,
);When wp_query is called and the posts are displayed they are still out of order. I have tried to debug this by checking the SQL that is called from the wp_query using:
$query = new WP_Query( $args );
echo $query->request;The SQL query looks like this:
SELECT SQL_CALC_FOUND_ROWS wp_posts.id
FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.id = wp_postmeta.post_id )
WHERE 1 = 1
AND ( wp_postmeta.meta_key = ‘_custom_order’ )
AND wp_posts.post_type = ‘download’
AND ( wp_posts.post_status = ‘publish’
OR wp_posts.post_status = ‘refunded’
OR wp_posts.post_status = ‘failed’
OR wp_posts.post_status = ‘revoked’
OR wp_posts.post_status = ‘abandoned’
OR wp_posts.post_status = ‘active’
OR wp_posts.post_status = ‘inactive’
OR wp_posts.post_status = ‘private’ )
GROUP BY wp_posts.id
ORDER BY wp_posts.menu_order,
wp_postmeta.meta_value + 0 DESC
LIMIT 0, 10When I run this directly in the mysql terminal I get this message:
ERROR 1055 (42000): Expression #2 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column ‘wordpress.wp_postmeta.meta_value’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
At this point I’m not sure why it is not working with the arguments I put in the $args array. Any pointers for debugging this or pointing me to a resolution is much appreciated.
The topic ‘Ordering wp_query posts from custom field not working’ is closed to new replies.