1

I have made a custom post type (sidebar_element) which are shown on sidebar. On each sidebar element admin can choose where in sidebar should that element be shown, ie. 'top', 'middle' or 'bottom', and that the value is saved in meta field. Meta values for those alignments are 30, 20 and 10.

Now, if there are multiple sidebar elements with same alignment then they should be aligned by their menu_order value given.

This is how I'm trying to achieve all that, in my sidebar.php

$sidebarElems   = new WP_Query( array (
                        'post_type'         => 'sidebar_element',
                        'posts_per_page'    => -1,
                        'meta_key'          => 'sidebar_element_meta_alignment',
                        'orderby'           => 'meta_value_num menu_order',
                        'order'             => 'DESC' ) );

This however isn't working. Elements get sorted first by meta_value_num ascending and then, correctly, by menu_order. If I delete the menu_order from orderby, meta_values gets properly ordered.

This thing is driving me crazy!

Please help. Thanks.

EDIT
Here's the SQL Query generated, when var_dump($sidebarElems)

SELECT XXX_posts.* FROM XXX_posts
INNER JOIN XXX_postmeta ON (XXX_posts.ID = XXX_postmeta.post_id)
WHERE 1=1 AND XXX_posts.post_type = 'sidebar_element' AND (XXX_posts.post_status = 'publish') AND (XXX_postmeta.meta_key = 'sidebar_element_meta_alignment' )
GROUP BY XXX_posts.ID
ORDER BY XXX_postmeta.meta_value+0,XXX_posts.menu_order DESC

ANSWER
Found an answer, check below

2
  • If you var_dump $sidebarElems, i think you should see the raw SQL somewhere. What does that look like?
    – vancoder
    Commented Mar 1, 2013 at 17:41
  • @vancoder Hi, edit my question to include the SQL query. Do you see anything suspicious?
    – micadelli
    Commented Mar 4, 2013 at 8:14

1 Answer 1

1

Found an answer to this.

Somehow WordPress' own orderby isn't fully functional when trying to order by multiple values.

I solved this by changing WP_Query to SQL Query, and including second DESC after _postmeta.meta_value+0

SELECT XXX_posts.* FROM XXX_posts
INNER JOIN XXX_postmeta ON (XXX_posts.ID = XXX_postmeta.post_id)
WHERE 1=1 AND XXX_posts.post_type = 'sidebar_element' AND (XXX_posts.post_status = 'publish') AND (XXX_postmeta.meta_key = 'sidebar_element_meta_alignment' )
GROUP BY XXX_posts.ID
ORDER BY XXX_postmeta.meta_value+0 DESC,XXX_posts.menu_order DESC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.