WordPress.org

Ready to get started?Download WordPress

Forums

WP_query simply doesn't work with orderby => ID (2 posts)

  1. EStephenBurnett
    Member
    Posted 2 years ago #

    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:

    <?php
    	$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>
    		</div>
    		<?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.

  2. vtxyzzy
    Member
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic