Alphabetical sorting of custom post type – one letter per page

Basically what I am looking for is a way to sort custom post type in alphabetical order and separate each letter, so you have one letter per page. See illustration. Hopefully there is a plugin or another genious way to do this.

Link to illustration -> http://postimg.org/image/5yyhidf7n/

Related posts

1 comment

  1. Building the navigation is pretty straight forward. You have a few possibilities afterwards, I’d suggest you build one page template and use a GET-parameter to sort it out.

    using a GET variable

    Your Links in the navigation would be like: http://www.example.com/yourpage?letter=a

    In the page template you make a simple query in the wpdb.

    global $wpdb;
    $yourposts = $wpdb->get_results( 
        $wpdb->prepare( 
            "
                SELECT * FROM $wpdb->posts
                WHERE post_title LIKE %s
                AND post_type = 'yourposttype'
            ",
                $_GET['letter'] . '%' 
            )
    );
    

    You can just loop through the Array that is returned here.

    using a custom post type

    You could also create a Custom post type alphabet or something like that. Create each Post in this CPT with the Letters of the alphabet (‘a’, ‘b’, …)

    This results in URLs like http://www.example.com/alphabet/a, and it is also really easy to build the alphabetical navigation with it.

    In your single-alphabet.php you use the code above, but replace the $_GET['letter'] with get_the_title().
    Of course, you can also achieve this using metadata, but the solution using a custom post type is quite elegant, as you get your URL structure delivered nicely without any effort.

    using a taxonomy

    last, this is another way to go, but not really practical as you always have to assign your the taxonomy according to the title (post ‘banana’ has to get the taxonomy ‘b’).

    Depending on what you try to achieve, I’d go with the version of the custom post type.

Comments are closed.