WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_query ordered by post ID but not date (7 posts)

  1. ahmadj
    Member
    Posted 3 years ago #

    Hi all,

    I'm setting up a website for the distribution of audio lectures. I'm using 'date' to show the date of recording, not the date of posting. However, I also want a 'latest posts' custom query - ie. I need a way to order the posts by post_id instead of date.

    I'm using the following to set up my 'latest posts' loop:
    <?php $custom_loop = new WP_Query('showposts=10&cat=-167&orderby=id&order=DESC'); ?>

    All of the above arguments work, except 'orderby=id'. How would I get the posts to be ordered numerically by ID?

  2. MichaelH
    Member
    Posted 3 years ago #

    This worked for me though I'm excluding category 1 and displaying all posts per page:

    <?php
      $args=array(
        'category__not_in' => 1,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'orderby'=> 'id',
        'order' => 'desc',
        'ignore_sticky_posts' => 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
          echo 'post id: ',$post->ID;
        endwhile;
      }
    ?>
  3. ahmadj
    Member
    Posted 3 years ago #

    I copied your code verbatim but it doesn't seem to work... I tested by creating new posts and setting the dates far in the past. These posts have higher IDs than the others, but still appear further down the list, in date order.

    Any idea what could be the issue?

  4. MichaelH
    Member
    Posted 3 years ago #

    Are you sure you are putting the code in the proper place? Do you see the 'post id: 123' or whatever displayed?

  5. ahmadj
    Member
    Posted 3 years ago #

    Yup, I see the post IDs displayed. That's how I can tell the ordering is not working. Have you tested it with out-of-order-ID-compared-to-date posts?

    This is strange... Could it be a bug in WP 3.1.1?

  6. MichaelH
    Member
    Posted 3 years ago #

    Nope it's my fault. That should read:

    'orderby'=> 'ID',

    Note the capitalization of ID

    <?php
      $args=array(
        'category__not_in' => 1,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'orderby'=> 'ID',
        'order' => 'desc',
        'ignore_sticky_posts' => 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
          echo 'post id: ',$post->ID;
        endwhile;
      }
    ?>
  7. ahmadj
    Member
    Posted 3 years ago #

    Magnifique! It works! Thanks :)

Topic Closed

This topic has been closed to new replies.

About this Topic