Create an Automatically Updating Review Index by Book Author

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.

You may also want to check out my other tutorial on how to create an automatically updating review index by book title. This one is just for book authors!

I honestly can’t accept any form of credit for this snippet of code that I’m about to show you. I knew what I had to do, but I’m not an expert with PHP and wasn’t completely sure how to do it. But my amazing boyfriend put this together for me and I’m incredibly grateful! So all coding credit goes to him! Love you, babe ♥

There are a few requirements and things you should know before copying this code:

  • As previously stated, this is intended for WordPress users. Something similar might be achievable in Blogger, but I don’t know.
  • You need the ability to edit your theme files or insert PHP code.
  • This specific code snippet requires that you use the WordPress Custom Fields plugin. Here’s a guide on how to set it up. It’s not required that you have the information displayed, but it is required that the plugin is installed and that you have at least one custom field for “Author.” This field must be filled out in order for books to be indexed.
  • Your reviews must be “tagged” with the author’s name. The way the author’s name is written in the tag must be the same as how it is entered in the Custom Field. (i.e. if it’s tagged as J.K. Rowling, it will NOT work if it’s entered as “J Rowling” in the Custom Field. They must be the same.)
  • Your tag URL structure must be: http://www.yoursitename.com/tags/tag-name-here, where “yoursitename” is your domain name and “tag-name-here” is the name of a tag. If yours is different, you can change the code to accommodate that, but it means you won’t be able to copy and paste the code and have it immediately work.

How it will work

Take a look at my reviews by author index for an example of how it will look. Basically, we’ll be compiling a list of all the authors that appear on your blog for a book you have reviewed. Then, we link to the tag of that author. If you have tagged and labelled your reviews correctly, that tag archive page should show all your book reviews (or other posts) that have been tagged with that author’s name.

This works out well for me since I avoid tagging authors’ names in non-review posts. But if you often tag non-review posts (Waiting on Wednesday, In My Mailbox, other memes, and giveaways), this might not work as well as you’re hoping. It just means that when someone clicks on an author’s name in the index, the results will contain more than just book reviews.

All authors are sorted by last name. So “Robin LaFevers” will appear under “L” for “LaFevers.” And they’re also displayed as Last Name, First Name. So “Robin LaFevers” will appear under “L” as “LaFevers, Robin”. The way the code is written is that it will grab the last word in the Author Custom Field, put that at the beginning, add a comma, and then insert the rest of the name.

1. Create a New Page for the Review Index

You will need a new page for housing this review index. Create a new one and name it appropriately (like “Review Index by Book Author” :P).

2. The Code

Just like in the last tutorial, there are a few different ways you can insert the code. I chose to create a new page template for my theme called “page-reviews-author.php”. 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 Author')) {  } ?>

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

The important thing is that you need this code on your page:

<?php
	// Get all posts
	$posts = get_posts(array(
			'numberposts' => -1,
			'post_type' => 'post',
			'meta_key' => 'author',
			));

	$author_array_fixed = $author_array_raw = $author_letters_existing = array();

	$i = 0;
	foreach ($posts as $post) {
		// Grab the author name
		$raw_author = get_post_meta($post->ID, 'author', true);
		// Convert the name
		$author_split = explode(" ", $raw_author);
		if (count($author_split) == 1) {
			$author_fixed = $author_split[0];
		}
		else {
			$author_fixed = $author_split[count($author_split) - 1].", ";
			$author_fixed .= implode(" ", array_slice($author_split, 0, count($author_split) - 1));
			$author_fixed = trim($author_fixed);
		}
		if (!in_array($author_fixed, $author_array_fixed)) {
			// Add to arrays
			$author_array_fixed[$i] = $author_fixed;
			$author_array_raw[$i] = $raw_author;
			if (!in_array(strtolower(substr($author_fixed, 0, 1)), $author_letters_existing)) {
				$author_letters_existing[] = strtolower(substr($author_fixed, 0, 1));
			}
		}
		$i++;
	}
	natcasesort($author_array_fixed);
	// Now we have all the authors
	foreach (range('a', 'z') as $alpha) {
		echo '<h4>'.strtoupper($alpha).'</h4>';
		if (in_array($alpha, $author_letters_existing)) {
			echo '<ul>';
			foreach ($author_array_fixed as $id => $author) {
				if (strtolower(substr($author, 0, 1)) == $alpha) {
					// List the author
					echo '<li><a href="/tag/'.str_replace(' ', '-', $author_array_raw[$id]).'">'.$author.'</a></li>';
				}
			}
			echo '</ul>';
		}
	}

?>

Yikes, that’s crazy, isn’t it? Honestly, I’m not even going to try to break it down for you… it’s crazy and it just makes me drool over how awesome my boyfriend is for being able to write that! If you want to understand what’s going on, just reread How It Will Work.

3. Upload & Test It Out!

Make sure you upload your new template file (if you made a new one) to the proper directory. Then, you’ll have to go back to your page for this index and set it so that it’s using your new template.

Then, edit one of your old book reviews and fill out the custom field box to enter in the “Author.” Also make sure that the post is correctly tagged with the author’s name (spelled the same way as in the custom field). Then, view the Reviews by Author page and see if it’s displaying the author!

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

25 comments

  1. this is such an excellent tutorial! i’m stuck on one thing tho – you say “The β€˜Reviews by Author’ should be whatever you titled your page that will be displaying this index. Then you could include the code inside the { brackets }”. Exactly what code are you putting inside those brackets? I’m a bit lost there – I’m editing the page template instead of creating a new one, and while I have that code there, and then pasted the big code section on the new page, the page is just displaying the code.

    1. Paste the really long big of code inside those brackets. But since you’re doing it that way, you need to remove the tags (at the very beginning of the long bit of code, and at the very end). Because if you’re adding in that “if” statement then you already have PHP tags open and you don’t need to open them again!

      1. OH! I feel like a total idiot now. LOL. I actually thought the huge code was meant to go on the page I created for the index. No wonder it didn’t work! :headdesk: Thank you!! It works perfectly now! πŸ˜€ Your site is absolutely brilliant – i spent about 3 hours here tonight going through all your tuts and tips. I’m still building my blog and your site has a wealth of information for me. πŸ˜€

  2. Great tuts. But I have 1 issue. Normally I am pretty good with php, but there is something I can’t figure out. The ‘book reviews by author’ page just has the letters of the alphabet, no links.
    I tag all my authors as ‘author: firstname surname’ so can I change something in the coding to make it work? I tried tagging them as ‘firstname surname’ but that still doesn’t link to anything. The ‘author’ field in the custom fields is the same as it is in your set up, because I figured that would be easiest.
    The title one worked perfectly though. Maybe I am just too tired.

    Chantelle recently posted: Book Review: Whose Body?
    1. Oops! That was slightly incoherent wasn’t it. What I mean is that although the index shows up on my ‘Book Reviews by Author’ page, there are no links. I am assuming this is because I tag everything ‘author: firstname surname’, but then when I changed them to just ‘firstname surname’ there still seems to be an issue somewhere.

    2. It looks like your page isn’t picking up anything being in the author field. Do you definitely have the ACF author fields set up *and filled out*?

        1. I’m getting 403 Forbidden errors for both of those images. :/ It’s kind of hard to figure out what the problem is without seeing your custom fields set up and how you’re entering in the information.

          1. Argh! I’ll upload them to my photo bucket account and redo the links later when I am on the computer later this afternoon (Can’t do it from mum’s iPad). Stupid website.

                1. There must be something wrong with your code then because I just tested it using those same variables and it works perfectly fine!

  3. This specific code snippet requires that you use the WordPress Custom Fields plugin.

    What if I already have custom fields for items such as author, genre, series created within my theme already, would this code work if I used the meta title of those?

    1. It completely depends on how your theme is set up to do it. You could certainly try it and see if it works, but if not, I wouldn’t really be able to dig into your theme code to debug it for you. πŸ™

      1. Thank you! I am just so frustrated trying to find php code that will accomodate custom field types, or a plugin that will accomplish the same thing. Since they are already set, I don’t want to have to create new fields and then touch every single review πŸ™

        I have 4 custom taxonomies, Genre, Author, Series & Publisher. Each taxonomies can then have however many entries I chose. Once created, they look like this http://www.mysiteurl.com/book-book-author/gina-robinson. I’m just confused on if the code should point to the custom field _book_author or the entry within it, or if it will even work πŸ™

        1. The code would take some rewriting anyway to get it to work with custom taxonomies. Custom fields and custom taxonomies are two different things!

    1. This bit of code isn’t really written to support multiple authors. You certainly could just type both author names in the author box, but then they’ll be organized a bit weirdly and won’t appear under two separate entries (one for each author’s name).

Recent Posts

    Random Posts