$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - List posts in alphabetical order|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - List posts in alphabetical order

matteradmin10PV0评论

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>

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>
Share Improve this question edited Dec 1, 2014 at 5:12 Pieter Goosen 55.5k23 gold badges117 silver badges211 bronze badges asked Nov 25, 2014 at 11:20 Kenny Van SittertKenny Van Sittert 451 gold badge2 silver badges5 bronze badges 1
  • 1 Please explain more. Where are you doing that? – Robert hue Commented Nov 25, 2014 at 11:21
Add a comment  | 

3 Answers 3

Reset to default -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

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' );
    }
});

You can use this plugin: GR Order Category Post (https://wordpress/plugins/gr-order-category-post/)

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far