• 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, 10

    When 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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • When you see an underscore in front of a meta key like:

    ‘meta_key’ => ‘_custom_order’,

    you are dealing with a protected meta key, which limits what types of queries it will be available to. Unfortunately, that’s about all I know on the topic, but likely it’s the reason why your query isn’t working as expected.

    Thread Starter thairish

    (@thairish)

    Hi Paul,

    Thanks a lot your information helped me resolve it. I changed the meta_key to “custom_order” for my custom field and the query ordering worked! Saved me hours of headaches trying to figure this out.

    If it helps anyone my updated $args array for the query is:

    $args = array(
    ‘post_type’ => ‘download’,
    ‘meta_key’ => ‘custom_order’, //Changed the meta key to not have the starting underscore
    ‘orderby’ => ‘meta_value_num’,
    ‘order’ => ‘DESC’,
    );

    Regards,

    James

    • This reply was modified 9 years, 9 months ago by thairish.
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Ordering wp_query posts from custom field not working’ is closed to new replies.