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
3 Answers
Reset to default -3Set 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/)