Archived topic
Arrange posts within a category alphabetically
11 replies · Started by Robert on May 11, 2018
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?
Hi there,
This article should help: https://codex.wordpress.org/Alphabetizing_Posts
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!
No problem :)
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
- "><?php the_title(); ?>
// 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 endforeach; ?>
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' );
Thank you for the elaborate answer!
So the last snippet i can directly put using code snippets plugin php section?
Hi Liis,
Yes, you may.
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' );
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.
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?
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