Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Below is my category.php page code. I want to order by ASC. How can I achieve this?

<div class="content">
  <?php if ( have_posts() ) : ?>
  <h2>    <?php single_cat_title('',true); ?>  </h2>
<ul class="products">
<?php while ( have_posts() ) : the_post();

    ?>

    <li> <a href="<?php 
    $url = get_post_meta($post->ID, 'productURL', true); 

    if ($url) {
        echo "$url";
    }
?>" target="_blank">
      <?php  the_post_thumbnail() ?>
      <h3><?php the_title(); ?></h3>
      <?php endwhile; 
            ?>
      </a></li>

  <?php else : ?>
  <?php endif; ?></ul>
</div>
share|improve this question

1 Answer 1

up vote 0 down vote accepted

EDIT

When you decided to unaccept my answer and change your question in the comments to my answer I was already in bed. Please consider @kaiser comment next time when asking a question.

For the purpose of my answer, here is your comment

i am having problem.. actually i want to show order by alphabet . like A B C....but it not working that way. any idea how

$query->set( 'orderby','title', 'order', 'ASC' ); not work

You cannot pass two arguments to $query->set. You need to add each argument separately, so to achieve your goal, this is how your code will look like

function wpse_asc_cat_pages( $query ) {
    if ( $query->is_category() && $query->is_main_query() ) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'wpse_asc_cat_pages' );

ORIGINAL ANSWER

You can achieve this by simply using pre_get_posts

This code goes in to your functions.php. This function checks whether you are on a category page and if this is the main query, and if both exists, it orders the posts ASC

function wpse_asc_cat_pages( $query ) {
    if ( $query->is_category() && $query->is_main_query() ) {
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'wpse_asc_cat_pages' );
share|improve this answer
    
thanks.. it work. –  Accore LTD May 8 at 19:48
    
i am having problem.. actually i want to show order by alphabet . like A B C....but it not working that way. any idea how –  Accore LTD May 8 at 20:01
    
$query->set( 'orderby','title', 'order', 'ASC' ); not work –  Accore LTD May 8 at 20:06
1  
@AccoreLTD (1) There is a "help" link below "Add Comment". Please use it to gain the ability to write properly formatted comments. (2) "not work" is a problem description that ... does "not work". It's too vague. What does not work? Expectations vs. Outcome ... as edit to your question please. (3) Please be more accurate when asking questions. Else you are wasting the time people invest in trying to help you like Pieter Goosen just did. Thanks for keeping that in mind for this and further questions. –  kaiser May 9 at 0:55
    
Thanks for your comment @kaiser +1 –  Pieter Goosen May 9 at 4:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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