1

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/

  • What have you tried so far? Showing your research and your efforts in code is a prerequisite. Due to the lack of that, your question currently is just a task and much too broad. – kaiser Jan 27 '14 at 11:02
  • @kaiser I have a non-hierarchical CPT called fleet. I want to make the Archive to work as the image, where the posts sorts by letter. I haven't done anything on the actual sorting code, because I don't know if i should use something like MixItUp.js or use Wordpress for what it's worth. It can for UX purposes make it as a jQuery-portfolio-sorting-thing, but I am still not sure how to proceed... – Magnus Østrem Jan 27 '14 at 11:09
  • Please not as a comment, but as an edit. – kaiser Jan 27 '14 at 11:18
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.

  • I have a non-hierarchical CPT called fleet. I want to make the Archive to work as the image, where the posts sorts by letter. I haven't done anything on the actual sorting code, because I don't know if i should use something like MixItUp.js or use Wordpress for what it's worth. It can for UX purposes make it as a jQuery-portfolio-sorting-thing, but I am still not sure how to proceed... – Magnus Østrem Jan 27 '14 at 11:07
  • +1 but don't forget escaping GET/POST/REQUEST stuff :) – kaiser Jan 27 '14 at 11:18
  • @kaiser I was under the impression that prepare handles that for me? – fischi Jan 27 '14 at 11:27
  • It does a mysql_real_escape_string(). Better be on the safe side. – kaiser Jan 27 '14 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.