WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 20 months ago

#17125 closed defect (bug) (worksforme)

orderby is ignored on some pages/queries

Reported by: majestictreefrog Owned by:
Milestone: Priority: normal
Severity: major Version: 3.1
Component: Query Keywords: reporter-feedback
Focuses: Cc:

Description

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

Change History (5)

comment:1 majestictreefrog3 years ago

  • Cc majestictreefrog added

comment:2 scribu3 years ago

It would be easier to test this if you also provided the output of print_r($newOrder);

Last edited 3 years ago by scribu (previous) (diff)

comment:3 scribu3 years ago

  • Keywords reporter-feedback added

comment:4 scribu3 years ago

I did a preliminary test and I can't reproduce.

The ORDER BY clause seems to have been completely disabled.

Make sure it's not caused by a plugin or some other code in your theme.

Version 0, edited 3 years ago by scribu (next)

comment:5 scribu20 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

No response in over a year. Closing.

Note: See TracTickets for help on using tickets.