1

What I'm trying to achieve is to set the list shown in alphabetical order. Any thoughts on how i can successfully achieve this?

I have pasted the snippet below for reference.

 <ul class="hotel-list">
                  <?php while (have_posts()) : the_post(); ?>
                    <?php
                    $short_description = get_post_meta(get_the_ID(), 'short_search_description', true);
                    $destination = get_post_meta(get_the_ID(), 'destination', true);
                    ?>
                    <li>
                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(250, 9999)); ?></a>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); echo $destination; ?></a>
                        <p><?php echo $short_description; ?></p>
                        <div class="clear"></div>
                    </li>
                  <?php endwhile
                  ; ?>
               </ul>
  • 1
    Please explain more. Where are you doing that? – Robert hue Nov 25 '14 at 11:21
-3

Set below code :

 <?php if (is_category()) { $posts = query_posts($query_string .'&orderby=title&order=asc'); } ?>

You can exclude if condition if do not using category.

For more reference: Alphabetizing Posts

  • Can down voter explain why down vote? – Helping Hands Nov 25 '14 at 11:56
  • 2
    Never use query_posts. Also, in a case like this, never jump to conclusions and use a custom query, it can be a case where you could have just modified the main query with pre_get_posts – Pieter Goosen Nov 25 '14 at 11:57
  • @PieterGoosen - I have also given Wp codex reference if my code is not as in standard. So I dont think its way to down vote my given answer. – Helping Hands Nov 25 '14 at 11:58
  • 2
    Your answer should not rely on an external link, and in my opinion, that codex page is really poorly written – Pieter Goosen Nov 25 '14 at 12:05
  • 1
    That's why I was asking him to explain his question because it can be done with pre_get_posts very easily. Just wanted to make sure before I suggest. – Robert hue Nov 25 '14 at 12:08
2

On the contrary to what is suggested by the accepted answer, you should never use query_posts

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

Also, looking at your code, this is the main query on some template, and as you did not take the time to even reply to @Roberthue comment to the OP, I still don't know which template. This brings me to the following, never replace the main query for any type of custom query on any archive page or the home page. Custom queries may only replace the main query on page templates. Please see this post which should explain everything to why I have made that statements

You could have simply just used pre_get_posts to change your post order on the specific template in conjunction with the conditional tags

Example to change the post order on a category page

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

If you need this on all templates, you can do something like this

add_action( 'pre_get_posts', function ( $query ) {
    if ( !is_admin() && !$query->is_page() && $query->is_main_query() ) { //Exclude page templates
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
});
0

You can use this plugin: GR Order Category Post (https://wordpress.org/plugins/gr-order-category-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.