2

I am using the following but my posts are still in Chronological Order (Oldest to New). My target is to have latest post on the top. (Newest to Old)

$catquery = new WP_Query( array (
    'cat'=>'27', 
    'post_type'=>'news', 
    'orderby' => "post_date", 
    'order' => "DESC" ) 
);

while($catquery->have_posts()) : $catquery->the_post();

<p class="date"> <?php the_date(); ?> </p>

<h3><?php the_title(); ?></h3>

<p> <?php the_content('Read More', FALSE); ?> 

I have also tried orderby' => "date" but no luck. How to solve this?

7
  • 5
    Shouldn't it be ASC?
    – elclanrs
    Sep 9, 2013 at 23:19
  • @elclanrs as per my knowledge "The ASC option specifies an index maintained in ascending order; this is the default order. The DESC option can specify an index that is maintained in descending order" My target is to have latest post on the top and so on .. Thank You for your Response
    – Shiva
    Sep 10, 2013 at 12:02
  • 1
    try 'orderby' => "date"
    – Kamil
    Sep 10, 2013 at 12:13
  • @Kamil I tried the same as well but no luck
    – Shiva
    Sep 10, 2013 at 12:34
  • you have a use custom post type news?
    – Ravi Patel
    Sep 10, 2013 at 12:57

2 Answers 2

1

Your code is close, but there are a few problems.

  1. 'cat' expects an int not a string so you need 'cat'=>27,
  2. Instead of post_date you need date
  3. I'm not sure which order you need so try ASC if DESC doesn't work.

Here's the new query:

$catquery = new WP_Query(array (
  'cat'       => 27, 
  'post_type' => 'news', 
  'orderby'   => 'date', 
  'order'     => 'DESC'
));
  • order (string) - Designates the ascending or descending order of the 'orderby' parameter. Defaults to 'DESC'.
    • 'ASC' - ascending order from lowest to highest values (1, 2, 3; a, b, c).
    • 'DESC' - descending order from highest to lowest values (3, 2, 1; c, b, a).

Here's a reference: WP_Query

10
  • I have already tried this, you can see last line of my question I have also tried orderby' => "date" but no luck. Thank You @graham
    – Shiva
    Sep 10, 2013 at 12:30
  • @Shiva are you sure news is a post type and not a category or tag? I know it's possible to have custom post types, but I'm just checking Sep 10, 2013 at 12:34
  • Yes news is post type wp-admin/post-new.php?post_type=news this is the url of new news item.
    – Shiva
    Sep 10, 2013 at 12:37
  • Hold on you want old -> new, you need ASC. I misread your question. I've updated my answer Sep 10, 2013 at 12:44
  • My target is to have latest post on the top. New ---- > Old My question title is Getting Latest Post on top (Reverse Chronological Order) Even after that I tried ASC But still no luck Thank you
    – Shiva
    Sep 10, 2013 at 12:53
1
    your post is custom post type so use this argument:'
    <?php 
    $args = array(
        'tax_query' => array(
                array(

                'taxonomy' => 'news_category',

                'field' => 'id',

                'terms' => '27'

            )
        ),
        'post_type'=>'news',
        'order_by'=>'date',
        'order'=>'DESC',
        'posts_per_page'=>-1
);
    query_posts($args);
    while ( have_posts() ) : the_post(); 
?>
    <li>
        <p class="date"><?php echo get_the_date('F j, Y'); ?></p>
        <a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
    </li> 
<?php 
endwhile;
wp_reset_query();
?>
3
  • Thank You @ravi but unfortunately this 'tax_query'is not able to fetch the posts. Am I missing any specific ID or line.. No post is being display after using this query.
    – Shiva
    Sep 10, 2013 at 13:22
  • Thank You for your effort http://xxxxxx/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=27&post_type=news I have put 'taxonomy' => 'category' Now post are being display on the page but new post --- > old old order is not there.
    – Shiva
    Sep 10, 2013 at 14:35
  • 1
    post per page in change you display no of post and add after endwhile <?php twentythirteen_paging_nav(); ?>
    – Ravi Patel
    Sep 11, 2013 at 4:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.