2

i have this snippet for specific category it works perfect but i want little modification to show posts by Ascending order.

<?php

 // The Query
 query_posts( array ( 'category_name' => 'A', 'posts_per_page' => -1 ) );

 // The Loop
while ( have_posts() ) : the_post(); ?>
   <li>
     <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
 </li>

 <?php endwhile;

 // Reset Query
 wp_reset_query();

 ?>
1

Pretty sure query_posts is the worst way to query...

Always use get_posts from what I'm constantly told. Remove the arguments that you don't use on the array below.

$args  = array(
    'posts_per_page'  => 5000,
    'offset'          => 0,
    'category'        => ,
    'orderby'         => 'post_date',
    'order'           => 'ASC',
    'include'         => ,
    'exclude'         => ,
    'meta_key'        => ,
    'meta_value'      => ,
    'post_type'       => 'post',
    'post_mime_type'  => ,
    'post_parent'     => ,
    'post_status'     => 'publish',
    'suppress_filters' => true ); 
$posts = get_posts($args);
    foreach ($posts as $post) :
    ?><div class="">
        <a href="<?php the_permalink();?>">
          <?php 
               echo the_title();
               echo the_post_thumbnail(array(360,360));
               the_excerpt('more text');
          ?></a></div>
    <?php endforeach; ?>
<?php 

WP_query method with category id's:

$query = new WP_Query( array( 'category__in' => array( 2, 6 ), 'order' => 'ASC') );

Or change the query like this, but I don't know how to add ascending to it:

    add_action( 'pre_get_posts', 'add_my_custom_post_type' );

    function add_my_custom_post_type( $query ) {
        if ( $query->is_main_query() )
            $query->set( 'category', 'A' );
         // $query->set( 'order', 'ASC' ); maybe?
        return $query;
    }
1

Add 'order'=> 'ASC' to your query

 query_posts( array ( 'category_name' => 'A', 'order' => 'ASC', 'posts_per_page' => -1 ) );
0

Just add an argument to the array...

query_posts( array ( 'category_name' => 'A', 'posts_per_page' => -1, 'order' => 'ASC' ) );

...but don't use query_posts, see here why. Instead you could do this:

$args = array(
    'category_name' => 'A',
    'posts_per_page' => -1,
    'order' => 'ASC',
);

$your_query = new WP_Query($args);

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

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.