List Posts Alphabetically in one category (6 posts)

  1. markhem
    Posted 1 year ago #

    Twenty Twelve Child Theme

    I have read the codex http://codex.wordpress.org/Alphabetizing_Posts but I'm still not quite putting all the pieces together.

    My main menu has a category called 'Groups'. When users click on Groups the posts role out chronologically. I'd like them to role out alphabetically so I can list by locations that I put in my titles. For example;
    Kamloops - Pottery Association
    Kelowna - Pottery Club
    Vernon - Pottery Group

    I want to keep the thumbs and excerpts as they are, but change the order to alphabetical. I also want to do this with another category called 'Classes'.

    I'm not sure what I need to do, but I think I do this... Copy the category.php file into my child theme and add, just before the loop...

    // we add this, to show all posts in our
    // Groups sorted alphabetically
    if (is_category('Groups'))
    $args = array( 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC' );
    $groupsposts = get_posts( $args );
    foreach( $groupsposts as $post ) :	setup_postdata($post);
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>


  2. markhem
    Posted 1 year ago #

    So, I know that doesn't work, because I just tried it on my localhost. Can anyone point me in the right direction? I've read a lot of posts on this, but can't find a way to do it.

  3. alchymyth
    Forum Moderator
    Posted 1 year ago #

    try using an add_action with 'pre_get_posts' in functions.php of your theme; http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts


    function category_archive_sort_alphabetical( $query ) {
        if ( is_admin() || ! $query->is_main_query() )
        if ( is_category('Groups') ) {
    		$query->set( 'orderby', 'title' );
    		$query->set( 'order', 'ASC' );
    add_action( 'pre_get_posts', 'category_archive_sort_alphabetical', 1 );
  4. markhem
    Posted 1 year ago #

    Thanks @alchymyth for supplying some code and pointing me in the right direction in the codex. This is exactly what I have been looking for to sort my category posts, and its now working. All plugins are back on and no problems.

    If I want to add other categories would I add them this way?

    if ( is_category('Groups', 'Classes', 'Resources') ) {

    Or just repeat the if statement again. Its working when I repeat the if statement. Just wondering if its better to use only one if statement.

  5. alchymyth
    Forum Moderator
    Posted 1 year ago #

    if ( is_category( array('Groups', 'Classes', 'Resources' ) ) ) {


  6. markhem
    Posted 1 year ago #

    I see. Again, thanks for pointing me to the codex. The array works perfectly.

Topic Closed

This topic has been closed to new replies.

About this Topic