Create an Automatically Updating Review Index by Book Title

Note: This is somewhat of an older method to create a review index. It still works 100%, but if you want a much better, more user-friendly way to do it, consider purchasing the Ultimate Book Blogger Plugin. It comes with this review index already installed, plus a million more features!

Other Review Indexes

This guide is for WordPress users. I’m sure there are similar ways to achieve this for Blogger, but the code in this tutorial uses code and plugins specific to WordPress.

Interested in having a book review index by book title on your blog? In this post I’ll show you the steps I went through to create my review index. This index updates automatically so there is no need to manually edit in all the book titles you read!

Special thanks to my amazing boyfriend for helping me get this code together. When I did it myself, I just copied the same code over and over again for each letter. My boyfriend helped me turn that into a loop that automatically cycles through the entire alphabet. So now I have one block of code that takes care of the entire alphabet instead of 26 blocks of the same code for each letter.

Let’s get started.

1. Install the Advanced Custom Fields plugin

This plugin allows you to create new input options for each post you create. I created a new field group called Book Reviews. You can then set rules for when this group will show up on the post creation page. I set the rule to:

Post category is equal to Reviews

That means the input boxes will only show up if I create a new post in the “Reviews” category.

Inside the group I have fields for “Book Title,” “Author,” “Series,” “Genre,” and all the other pieces of information that show up with every single review I write. Then these fields show up, ready for me to fill out when I create a new post. But for this specific guide, you only need a field for “Book Title Letter.” Use the following settings:

Field Label: Book Title Letter
Field Name: book_title_letter
Field Type: Text
Field Instructions: What letter does the book title start with?
Required? Yes
Default Value: [blank]
Formatting: HTML

Then publish/save your field group.

Note: I would encourage you to also read my tutorial on How to Use Custom Fields for Entering Book Information

2. Create a New Page for the Review Index

Now create a new page. This page will be the one that displays your review index by book title so you will want to label it appropriately.

3. The Code

There are several different ways you can approach this next step. I personally created a new page template for my theme called “page-reviews-title.php” since it is a *page* for my reviews by title. If you do this, you’ll want to copy your page.php template file and paste the code we’ll be using (shown later) directly after this line:

<?php the_content(); ?>

Alternatively you can edit your page.php template file and run this if statement (also after the line above):

<?php if (is_page('Reviews by Title')) {  } ?>

The ‘Review by Title’ should be whatever you titled your page that will be displaying this index. Then you would include the code inside the { brackets }

The important thing is that you need one of the following bits of code on your page (pick which one you want):

Letters Only
<?php

	$alphabet_search = range('a', 'z');
	
	foreach ($alphabet_search as $alpha) {
		echo '<h4>'.strtoupper($alpha).'</h4>';
		$posts = get_posts(array(
			'numberposts' => -1,
			'post_type' => 'post',
			'meta_key' => 'book_title_letter',
			'meta_value' => $alpha
		));
		if($posts)
		{
			echo '<ul>';
		 
			foreach($posts as $post)
			{
				echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';
			}
		 
			echo '</ul>';
		}
	}

?>

First, we are defining our alphabet range (A – Z). Then, we cycle through each letter of the alphabet, collecting posts that match this criteria:

  • Is a post
  • Has the meta key “book_title_letter” (that’s the Custom Fields option we created)
  • The value of book_title_letter equals the letter of the alphabet that we’re currently cycling through

Then for the current letter we’re on, we create an unordered list with names and links to all the book reviews that begin with that letter. We continue this process, cycling through the alphabet, until we reach “Z.”

Letters & Numbers
<?php
	// Do numbers first
	$number_search = range(0, 9);
	$number_string = "";
	foreach ($number_search as $number) {
		$posts = get_posts(array(
			'numberposts' => -1,
			'post_type' => 'post',
			'meta_key' => 'book_title_letter',
			'meta_value' => $number
		));
		if($posts)
		{
		 
			foreach($posts as $post)
			{
				$number_string .= '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';
			}
		 
		}
	}
	if (strlen($number_string) > 0) {
		echo '<h4>#</h4>';
		echo '<ul>';
		echo $number_string;
		echo '</ul>';
	}

	$alphabet_search = range('a', 'z');
	
	foreach ($alphabet_search as $alpha) {
		echo '<h4>'.strtoupper($alpha).'</h4>';
		$posts = get_posts(array(
			'numberposts' => -1,
			'post_type' => 'post',
			'meta_key' => 'book_title_letter',
			'meta_value' => $alpha
		));
		if($posts)
		{
			echo '<ul>';
		 
			foreach($posts as $post)
			{
				echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';
			}
		 
			echo '</ul>';
		}
	}

?>

This alternative bit of code does the exact same as above, but then it also includes numbers. Here are some important things to keep in mind for this code:

  • All numbers are grouped together under one heading called “#” (you can change this to anything else though).
  • Since books beginning with numbers are so rare, I have it so that the heading for numbers (“#”) is hidden by default. All of the headings for letters show up regardless of whether or not you have an entry for that letter. But the heading for numbers will be hidden unless a book that begins with a number is in your index.
  • When filling out the Book Title Letter field for a number, you must enter in a value from 0-9. So if the book is “172 Hours on the Moon,” you would put in a “1”.

4. Test It Out!

If you created a page template like I did, you’ll want to go back to your page for this index and make sure it’s using your new template (if you chose to go the page template route).

Then, edit one of your previous book reviews. You should now have the custom field box to enter in the Book Letter Title. Enter in the first letter of the book title (it’s usually best to exclude “The”). So if my book title is “The Golden Compass,” I would enter in “g”.

Then view the Reviews by Index page and see if it’s displaying your review!

Unfortunately you will have to go back and edit every single book review post to add in the book letter title. But it will save you from a lifetime of manually updating your index!

Photo of Ashley
I'm a 30-something California girl living in England (I fell in love with a Brit!). My three great passions are: books, coding, and fitness. more »

Don't miss my next post!

Sign up to get my blog posts sent directly to your inbox (plus exclusive store discounts!).

You might like these

13 comments

    1. I’m glad you found this to be helpful! I hope my tutorial wasn’t too… scary. Sometimes it’s hard to put coding stuff into simple terms. At least for me it is.

      Stay tuned because I have two more similar tutorials coming out soon! They’re automatically updating review indexes by book *author* and then another one by *year*. Super useful 🙂 I love having one of each!

    1. I just added in a new bit of code to include numbers 🙂 Check out the post again and look for the heading called “Letters & Numbers” for the new bit of code!

      Let me know if you have questions!

    1. You can do some seriously super awesome automated things with Advanced Custom Fields! 😀 I LOVE LOVE LOVE it!

      This one might be a bit tricky to set up so feel free to post here or shoot me an e-mail if you need help. 🙂

    1. I’m starting to really love PHP! The possibilities are endless!

      Let me know if you need any help making this work! Hopefully my instructions are clear but you never know XD

      1. You would post both if statements! So you’d have one for if it’s the review by title page, with the code inside. Then you’d have another one after that for if it’s the review by author page!

        1. man, you’re good! LOL. I’m still stashing money aside so eventually I can get you to do my site. 😀 Couple of months maybe. So much easier than me tearing my hair out doing this. *laughs* Thank you! 😀

          Jaki recently posted: Suggestions?

Recent Posts

    Random Posts