0

I just purchased a WordPress theme. I’m trying to sort post on one category into an alphabetical listing (glossary type of listing).

Example : https://www.udiscovermusic.com/browse-artists/

There are using a WordPress theme, I just don’t know how to create that.

I have tried several plugins (like A-Z Listing and others) but it doesn't look good at all.

Could you please help me please ? Do you know a plugin or a way to create this please ?

thank you

2
  • Are you dealing with posts or custom post types? For custom post types, you might need to implement a different logic than for posts.
    – Badan
    Commented Aug 30, 2021 at 22:29
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Community Bot
    Commented Aug 31, 2021 at 9:16

1 Answer 1

0

You can change the sort order on a category archive page to show posts by title (A-Z) by adding this code to your child theme's functions.php:

/* Sort posts by title for a specific category */
function change_category_order( $query ) {
    //Sort all posts from category with id=3 by title
    if($query->is_category('3') && $query->is_main_query()){
        $query->query_vars['orderby'] = 'name';
        $query->query_vars['order'] = 'DESC';
    } 
}
add_action( 'pre_get_posts', 'change_category_order' );

You need to replace "3" with the correct category id. You can find out the category id by heading to dashboard => post => categories and hover over the category you want to adjust the sort order.

4
  • hi, there's a problem : Parse error: syntax error, unexpected 'elseif' (T_ELSEIF) in /home/customer/www/elontrap.com/public_html/wp-content/themes/smart-mag/functions.php on line 80
    – Marketboy
    Commented Aug 31, 2021 at 4:59
  • I am sorry, i made a typo. Replace the elseif with if. I updated my answer.
    – Badan
    Commented Aug 31, 2021 at 5:16
  • thanks but it doesnt do anything :/
    – Marketboy
    Commented Aug 31, 2021 at 5:29
  • That is why I asked you if it is a custom post type (e.g. artists instead of posts) and a custom taxonomy (e.g. genres instead of categories). This code should work only for posts from a specific category. For a custom taxonomy, you should use is_tax() instead of is_category. Let me know and i will update.
    – Badan
    Commented Aug 31, 2021 at 5:31

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.