• Resolved ahmadj

    (@ahmadj)


    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?

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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;
      }
    ?>
    Thread Starter ahmadj

    (@ahmadj)

    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?

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

    Thread Starter ahmadj

    (@ahmadj)

    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?

    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;
      }
    ?>
    Thread Starter ahmadj

    (@ahmadj)

    Magnifique! It works! Thanks ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘wp_query ordered by post ID but not date’ is closed to new replies.