I have written up a page template that will list posts in the same category as the name of the page. I am wondering how to alter it to get it to return the posts in alphabetical order?

I have been reading over and playing with how to change the template with the Alphabetizing Posts Codex Page. I just can't seem to get it to work. It would be greatly appreciated if someone demonstrated this for me once so I can see how it is done in this context.

The following is my code for the page template:

<?php /*
Template Name: Category Page

get_header(); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="content-container">
        <h1><?php the_title(); ?></h1>
            <?php the_content(); ?>
            <?php endwhile; 
            else: endif; ?>

<?php query_posts('category_name='.get_the_title().'&post_status=publish');?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>    </h1>
        <p><?php the_content(); ?>
<?php endwhile; else: endif; ?>

<?php /*If sidebar is not disabled in Customizer, get_sidebar*/
if ( get_theme_mod( 'ctheme_remove_sidebar' ) != 1 ) :
get_footer(); ?>

This is one way I'm thinking of doing it. If you have any other ideas how I should approach it, let me know!

The purpose of this page will be to retrieve a list of animal names, (no date for the post, no excerpt for the post, just the name of the animal) so alphabetical order would be the most useful.


  • 1
    To be really honest, that page is worth nothing and is really a bunch of crap. You should never ever use query_posts, it breaks the main query for one thing. Use WP_Query Nov 16, 2015 at 15:27
  • Thanks! Been reading over the Codex for it. Still pretty confusing but I'll continue trying to make it better.
    – Nimara
    Nov 16, 2015 at 16:11

2 Answers 2


As I have stated, never ever use query_posts. The page you have linked to in the codex is worth nothing and completely wrong. For extra info on why not to use query_posts, check my answer here and the answer by @Rarst.

You have another issue here, category_name does not accept the category name as value, but the slug. The naming convention of this parameter is wrong. get_the_title() returns the page name/title, not the slug. Your query might work if the page has a one word title without special characters as sanitizing the name in the WP_Tax_Query class might match it up the slug, but it will fail if the page name has special characters or have more than one word.

What you need is to get the page slug, which you will pass as category slug to category_name To do this, you need to get the queried object and then return the $post_name property. Again, the naming convention is completely wrong. $post_name holds the slug of the page, not the name.

In connection with ordering, you would need to set the orderby parameter to title and order to ASC to order from a-z and not the default z-a

All in all, your query would look something like this: (NOTE: Requires minimum PHP 5.4)

$args = [
    'category_name' => get_queried_object()->post_name,
    'order'         => 'ASC',
    'orderby'       => 'title' // Can use 'name' as well, see https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
    // Add any extra parameters you need
$q = new WP_Query( $args ); 

// Run the loop
if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {

        // Display what you need from the loop like title, content etc


You can also try to use something like this where you can have page where you choose which category to display when you publish a page, you can also select ordering and a few other features.

  • Thank you. This has definitely broadened my knowledge of both php and wordpress. I am now grasping why query_posts is extremely inferior to wp_query. The example went a long way in explaining what I needed and I will continuously check back on your posts for this matter as I continue to develop. Definitely less daunting now!
    – Nimara
    Nov 16, 2015 at 16:37
  • I'm glad I could be of help. Enjoy, and best of luck on your future projects. Enjoy ;-) Nov 16, 2015 at 16:40

You need to look closer at query_posts and order your posts by the title, as it says on the page that you linked to.

You need to change this:

<?php query_posts('category_name='.get_the_title().'&post_status=publish');?>

and add this:

<?php query_posts('category_name='.get_the_title().'&post_status=publish&orderby=title');?>

As someone else pointed out, you should really be using WP_Query in this situation. Take a look at the reference as many of the options are explained very clearly: Class Reference/WP Query

  • 1
    Never ever use query_posts, that page that you all linked to is total crap. You should be using WP_Query for custom queries Nov 16, 2015 at 15:28
  • Thanks. The Codex actually wasn't that straightforward to me which is why I came to ask it to be demonstrated. The $args array was throwing me off and I kept trying to insert the code above or before.
    – Nimara
    Nov 16, 2015 at 15:32
  • Thanks Pieter Goosen! Sorry I'm very new to this and I will attempt to change it promptly.
    – Nimara
    Nov 16, 2015 at 15:33
  • @PieterGoosen yes, philosophically you are right. But the OP asked "It would be greatly appreciated if someone demonstrated this for me once." So I am fulfilling their request, otherwise the person may become confused, discouraged, and give up all together.
    – pwbred
    Nov 16, 2015 at 15:33
  • 1
    @Nimara I have updated the answer with a link to the WP_Query documentation for future generations.
    – pwbred
    Nov 16, 2015 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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