Support » How-To and Troubleshooting » WP_query simply doesn't work with orderby => ID

WP_query simply doesn't work with orderby => ID

  • Usually I find the answer on an existing question. But this one has me stumped.

    This custom query is in my sidebar and works fine on every other page (including singles and custom post type archives) EXCEPT the index page and all the regular post type archives. Only there, the 'orderby' => 'ID' array parameter, or any other parameter such as 'order' => 'ASC', simply refuses to work. It will only display data by default date and descending order. Here’s the code for one of these custom queries:

    	$the_query = new WP_Query(array(
    		'post_type' => 'library',
    		'orderby' => 'ID',
    		'posts_per_page' => '1',
    	while ($the_query->have_posts()) : $the_query->the_post(); ?>
    		<div style="display: inline; padding: 3px 0 0 2px;">
    			<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('library-thumbnail'); ?></a>
    		<?php endwhile; wp_reset_query(); ?>

    By echoing the query request, I get this (only on index.php and all the regular post type archives):

    SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'library' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 0, 1

    So no plugins seem to be interfering. But it still only wants to “ORDER BY wp_posts.post_date DESC” and not “… wp_posts.ID”.

    Again, this query works fine and orders perfectly on all other pages except the index and blog archive pages. No plugins are interfering (as the query request echo indicates) and I reset the query every time.

    Much gratitude in response for any support.

Viewing 1 replies (of 1 total)
  • It seems to work for me in the Twenty Ten theme. Using this query:

    $the_query = new WP_Query(array(
    		'post_type' => 'parks',
    		'orderby' => 'ID',
    		'posts_per_page' => '1',
    	echo "<p>QUERY:$the_query->request</p>";

    I get this result:

    QUERY: SELECT SQL_CALC_FOUND_ROWS bmc_posts.* FROM bmc_posts WHERE 1=1 AND bmc_posts.post_type = 'parks' AND (bmc_posts.post_status = 'publish' OR bmc_posts.post_status = 'private') ORDER BY bmc_posts.ID DESC LIMIT 0, 1

Viewing 1 replies (of 1 total)
  • The topic ‘WP_query simply doesn't work with orderby => ID’ is closed to new replies.
Skip to toolbar