Template to List Posts By First Letter of Title, 1 Letter Per Page

This template is a variation of the one that shows posts or pages by first letter of title with a selected number of posts per page.

The template is written for the Twenty Ten theme.  You will probably need to modify it to tweak the appearance or work with a different theme.  The embedded CSS should really be moved to your stylesheet. Continue reading

List Post Titles Sorted on Custom Fields

The sample code here shows how to sort on the values of two Custom Fields, ‘Business Category’, and ‘Subcategory’, to produce a listing like this:


Auto Accessories

  • Acme Auto
  • Bleppo Auto Specialties

Auto Parts

  • Speedy Auto
  • Z’s Auto Parts

The approach is to use a custom query to select the posts and the two custom fields and specify the correct sort order.  The current category and subcategory are used to create control breaks for the proper styling.


// List posts by Business Category, Subcategory
$sql = "
SELECT p.*,m1.meta_value as category, m2.meta_value as subcategory
FROM $wpdb->posts p
JOIN $wpdb->postmeta m1 ON (p.ID = m1.post_id AND m1.meta_key = 'Business Category')
JOIN $wpdb->postmeta m2 ON (p.ID = m2.post_id AND m2.meta_key = 'Subcategory')
WHERE p.post_type = 'post'
   AND p.post_status = 'publish'
ORDER BY m1.meta_value, m2.meta_value, p.post_title
$all_posts = $wpdb->get_results($sql);
$curr_cat = '';
$curr_subcat = '';
$in_list = false;
foreach ($all_posts as $post) {
   $this_cat = $post->category;
   $this_subcat = $post->subcategory;
   if ($this_cat != $curr_cat) {
      $curr_cat = $this_cat;
      if ($in_list) {
         echo "</ul>\n";
         $in_list = false;
      echo "<h2>$curr_cat</h2>\n";
      $curr_subcat = '';
   if ($this_subcat != $curr_subcat) {
      $curr_subcat = $this_subcat;
      if ($in_list) {
         echo "</ul>\n";
      echo "<h3>$curr_subcat</h3>\n";
      echo "<ul>\n";
      $in_list = true;
   } ?>
   <li><a href="<?php echo get_permalink($postid); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php }
if ($in_list) echo "</ul>\n";


Shortcode Function to List Post Titles

The code below can be added to your functions.php to provide a shortcode which will list post titles.

When you add the code to functions.php, you must be careful, because any errors will make your site unusable.  BACK UP your functions.php before adding this, and make sure that you know how to restore from the backup.

If your functions.php file ends with ‘?>’, just paste the entire code on the line following it.  If it does not end in ‘?>’, remove the ‘<?php’ line and the ‘?>’ line from the code below before pasting it in.

Once the code has been added to functions.php with no errors, create a new Page to display your titles.  Enter any content you want on the page.  Where you want the list of titles to show, enter the shortcode as shown in the comments in the code. Continue reading

Template to List Posts By First Letter Of Title

Several persons have asked for a template to list posts or pages by the first letter of the title. The code below is set for posts, but is easily modified for pages or custom posts. The CSS will need to be modified to fit your own theme.

This template lists a set number of post titles per page.  See this Tip for a template that lists one letter per page.

Here is a screenshot of the first page:

Here is the pastebin link for the code below.
Continue reading

List Posts Alphabetically Under First Letter Of Title

Here is sample code to list posts alphabetically under the first letter of the title.

For a complete template, see this post.

Continue reading

List titles of all custom post types

The code below is from a post by MichaelH on the WP Support forum: Continue reading