[Resolved] Arrange posts within a category alphabetically

Home Forums Support [Resolved] Arrange posts within a category alphabetically

Home Forums Support Arrange posts within a category alphabetically

  • This topic has 11 replies, 4 voices, and was last updated 1 year ago by David.
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #573468
    Robert

    We have a category Country Profiles for which we now suppress showing the date using some CSS. We would like to arrange the posts within this category alphabetically by title. We can do it by assigning suitable dates to each post. But can this be automatically with some code?

    #573639
    Leo
    Staff
    Customer Support

    Hi there,

    This article should help: https://codex.wordpress.org/Alphabetizing_Posts

    #574011
    Robert

    Thanks, Leo!
    In our case we do not have that many posts in the category, so we are just assigning dates to achieve alphabetical order. Simple, but it works!

    #574043
    Leo
    Staff
    Customer Support

    No problem 🙂

    #2604449
    Liis

    sorry to jump on an old post.

    I add that php with code snippets “php” section and just change the ‘Glossary’ to ‘Uncategorized’ for example, i get a critical error.

    Am I doing it wrong with code snippets?

    <?php
    get_header();
    ?>

      <?php
      // we add this, to show all posts in our
      // Glossary sorted alphabetically
      if (is_category(‘Uncategorized’))
      {
      $args = array( ‘posts_per_page’ => -1, ‘orderby’=> ‘title’, ‘order’ => ‘ASC’ );
      $glossaryposts = get_posts( $args );
      }
      foreach( $glossaryposts as $post ) : setup_postdata($post);
      ?>

    • “><?php the_title(); ?>
    • <?php endforeach; ?>

    #2604727
    Ying
    Staff
    Customer Support

    The code is a PHP template, it’s not something you add to the code snippet.

    However, with GP premium and the Query loop block of GenerateBlocks plugin, you can achieve this without writing PHP code:

    1. Go to appearance > elements, create a block element, choose loop template as the element type.

    2. Add the GB query loop block, you can create whatever layout you want for the archive.

    3. Once the design is done, you can configure the query, choose the category, and add order and orderby parameters.

    4. Choose post category archive > your category as the location, and publish the element.

    Or

    you can simply add this function to code snippet:

    function my_category_orderby( $query ) {
        if ( $query->is_category( 'uncategorized' ) && $query->is_main_query() ) {
            $query->set( 'orderby', 'title' );
            $query->set( 'order', 'ASC' );
        }
    }
    add_action( 'pre_get_posts', 'my_category_orderby' );
    #2605039
    Liis

    Thank you for the elaborate answer!

    So the last snippet i can directly put using code snippets plugin php section?

    #2605053
    Fernando
    Customer Support

    Hi Liis,

    Yes, you may.

    #2608052
    Liis

    Thank you so much! Last question, if i would want to put a few categories, would just add then in ” next to ‘uncategorized’ ?

    like so:

    function my_category_orderby( $query ) {
    if ( $query->is_category( ‘uncategorized’ ‘category1’ ) && $query->is_main_query() ) {
    $query->set( ‘orderby’, ‘title’ );
    $query->set( ‘order’, ‘ASC’ );
    }
    }
    add_action( ‘pre_get_posts’, ‘my_category_orderby’ );

    #2608066
    Fernando
    Customer Support

    Something like this:

    function my_category_orderby( $query ) {
        if ( $query->is_category( array( 'uncategorized', 'category1' ) ) && $query->is_main_query() ) {
            $query->set( 'orderby', 'title' );
            $query->set( 'order', 'ASC' );
        }
    }
    add_action( 'pre_get_posts', 'my_category_orderby' );

    It’s inserted inside an array and separated by a comma.

    #2610338
    Liis

    that worked! fantastic thank you. Both my categories slug and name were the same. But in the future, what if my category name is reviews a to z. Should I put the name there or the slug reviews-a-to-z?

    #2610477
    David
    Staff
    Customer Support

    Hi there,

    you can put either the name, the slug or the id or any mixture of those in the is_category array

    i find the slug ( or ID ) less ambiguous so i generally stick to those when there is spaces in the name

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.