orderby is ignored on some pages/queries
|Reported by:||majestictreefrog||Owned by:|
This has happened on both 3.1 and 3.1.1
For example, take the following code (bug was reproduced with this) as a sample category.php code. This is specifically reproduced when viewing a subcategory of a larger category, but could be reproduced on multiple category pages.
<html> <head></head> <body> <?php $newOrder=array( 'meta_key'=>'post-order', 'orderby'=>'meta_value_num', 'order'=>'ASC' ); $newOrder=array_merge( $wp_query->query, $newOrder); print_r($newOrder); query_posts($newOrder); ?><pre><?php print_r($wp_query); ?></pre><?php if (have_posts()) :while (have_posts()) :the_post(); the_title(); echo " ->>>"; echo get_post_meta($post->ID, 'post-order', true); the_content(); endwhile;endif; get_footer(); ?> </body> </html>
yes, the code includes some reporting/debug statements. Assuming there is a custom meta field: post-order. This contains a number that is the order in which posts are to be displayed (e.g. 1, 2, 40, 1020, etc).
This query should return posts ordered by post-order.
the print_r($wp_query) statement returned the following SQL statement as what was run:
[request] => SELECT wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (25,70,71,72,73) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID
(25 is main category, 70, 71,72,73 are subcategories)
The CORRECT query would be:
SELECT wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND (wp_term_relationships.term_taxonomy_id IN (25,70,71,72,73) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND wp_postmeta.meta_key = 'post-order' GROUP BY wp_posts.ID order by 0+wp_postmeta.meta_value