• Hi everyone,
    I would like to have, for each category, an alphabetical index of all the posts of that category, like in this website (https://www.nerdface.it/hot-nerd/disney).

    I’ve tried different plugins like A-Z listing, but they allow to order all posts in the website and i can’t add a filter category like i want.

    I also tried with Taxonomy List Widget and give every post a tag with their initial, and it works but i still have the problem that are shown all posts and not only the posts of a certain category.

    Does anyone know how to fix this issue?

    Thank you

    • This topic was modified 3 years, 7 months ago by doorton.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Assuming you are able to code a little with PHP, ideally you’d query posts sorted by category term first, then secondarily by title. Unfortunately, you’d need to write your own SQL query, the usual WP_Query class doesn’t have a sort by term option. Here’s an existing example SQL query I had that could be adapted to your needs:

    global $wpdb;
    $query = $wpdb->get_results( "SELECT * FROM $wpdb->posts AS p
    	LEFT JOIN $wpdb->term_relationships AS r ON (p.ID = r.object_id)
    	INNER JOIN $wpdb->term_taxonomy AS x ON (r.term_taxonomy_id = x.term_taxonomy_id)
    	INNER JOIN $wpdb->terms AS t ON (r.term_taxonomy_id = t.term_id)
    	WHERE p.post_type IN ('post', 'portfolio')
    	AND p.post_status = 'publish'
    	AND x.taxonomy = 'category'
    	ORDER BY t.name ASC, p.post_title ASC;"
    );

    You could instead get all the category terms, then make a separate query for each term ordered by title. Not very efficient, but workable.

    Another option would be to take all the posts returned from the default query and sort them via a custom PHP sorting algorithm passed to usort(). Not a viable approach if you need to paginate the results.

    Thread Starter doorton

    (@doorton)

    Hi @bcworkz, thank you for the reply.
    Unfortunately I don’t know how to use PHP a lot. Even though I can adapt the code you provided me, I don’t know how to display the alphabet and related query pages in the various categories.
    Do you know if there are any plugins that can do this?
    Otherwise the only solution I have left is to do it by hand, but that means I would have to manually create 26 pages for each category, and that would be a terrible waste of time.

    Moderator bcworkz

    (@bcworkz)

    That kind of time would be better spent learning to code PHP ??
    Part of the problem is WP doesn’t really have a “post titles beginning with ‘X'” query. But a normal search query could be modified to remove the starting wildcard symbol. Instead of post_title LIKE %X%, make it post_title LIKE X%.

    With that adjustment, all the letter links could be like example.com/?s=X&category_name=disney.

    Maybe one of the advanced search plugins would get you closer to a solution. If the plugin supports GET queries, it’s be a matter of crafting appropriate letter links that fit the plugin’s GET query strings. Even generating the letter links could be coded, but manually creating them would still be better than manually creating full pages!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Alphabetical index of posts for each category’ is closed to new replies.