1

I'm having trouble getting my custom ordering to work. I'm trying to order a custom post type by a custom field called feature_order which is a numeric field (created with Advanced Custom Fields plugin).

Here is the code:

$args = array(
    'post_type' => 'attorneys',
    'posts_per_page' => -1,
    'meta_key' => 'feature_order',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
);

$query = new WP_Query( $args );

This has no effect on the actual order. The feature_order field was created correctly, and properly stores values, so that's not the issue.

Here's the SQL produced by when I use echo $query->request:

SELECT wp_posts.*
FROM wp_posts
INNER JOIN wp_postmeta 
ON ( wp_posts.ID = wp_postmeta.post_id ) 
WHERE 1=1
AND ( wp_postmeta.meta_key = 'feature_order' ) 
AND wp_posts.post_type = 'attorneys' 
AND (wp_posts.post_status = 'publish' 
    OR wp_posts.post_status = 'private') 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.menu_order, wp_postmeta.meta_value+0 ASC

The problem is the presence of wp_posts.menu_order in the ORDER_BY clause. When I execute the query directly on the database without this, I get the expected result.

How do I get WP_Query to stop ordering by wp_posts.menu_order?

Alernatively, how can I instantiate a WP_Query object with an SQL string?

Thanks in advance for your help!

  • 1
    I don't believe ordering by menu_order is a default behavior for WordPress, it should be post_date if anything which leads me to believe a plugin or a pre_get_posts hook is adding this in somewhere. I would start by disabling all plugins ( except the absolute necessary ) and then maybe searching the theme pre_get_posts. – Howdy_McGee Aug 24 '17 at 19:06
  • @Howdy_McGee Thanks for your help. You're correct. It was a plugin forcing ordering by menu_order. The plugin is called Post Types Order (wordpress.org/plugins/post-types-order), and this effect can actually be turned off in the settings. If you would put your response as an answer I would mark it as correct and you'd get a fancy, green checkmark ;) – Ron Aug 24 '17 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.