Alphabetizing Posts in a Category Page?

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; ?>
</div>

<?php /*If sidebar is not disabled in Customizer, get_sidebar*/
if ( get_theme_mod( 'ctheme_remove_sidebar' ) != 1 ) :
get_sidebar(); 
endif;
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.

Thanks!

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() ) {
    $q->the_post();

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

    }
    wp_reset_postdata();
}

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.

Leave a Comment