22 May

This is the second part of the tutorial ‘Linking WordPress posts using custom fields’. If you haven’t read part 1, you’ll need to take a look at that first here.

In part 1 we learned how to link to a post, and display it’s content within our new post using custom fields and manipulating the single.php file. In this part we are going to learn how to link back to the post using another single.php file. This is quite simple and because we have already setup our WordPress blog with the correct custom fields, the only file we will need to modify is the template file performer-single.php.

1. Requirements

  1. WordPress 2.7.1 (not tested in earlier versions)
  2. A basic understanding of PHP
  3. An understanding of the WordPress codex
  4. Read the first part of this tutorial here

2. Modify performer-single.php

Open performer-single.php from within your theme directory. This is the file we created earlier in part 1 of this tutorial.

We need to add the following code to our file in order to link them together. I’ve added my code on line 70, after the <?php endif; ?> line.

First off, let’s add a H2 tag to break up our new content.
<h2>Upcoming Performances from this Artist:</h2>
Rewind posts
<?php rewind_posts(); ?>
Now we need to get the title of the current post
<?php $my_title = single_post_title('', false); ?>
Create a new WordPress query. This queries only posts in the ‘events’ category, with a custom field with a value that matches the current post name, and return 10 results.
<?php $get_performer_events = new WP_Query('category_name=events&post_type=post&meta_value=' . $my_title . '&numberposts=10'); ?>
Start our WordPress loop
<?php while ($get_performer_events->have_posts()) : $get_performer_events->the_post(); $do_not_duplicate = $post->ID; ?>
Add some content to the loop. This will display the title, the content and the tags for each post that links to the current post.
<h2><a href="<?php the_permalink(); ?>" title="Link to: <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
<?php the_tags( '<p>Tags: ', ', ', '</p>'); ?>
<hr />

Now let’s end our loop
<?php endwhile; ?>

View our site for changes

Now let’s save our performer-single.php, and view our site for changes. If we take a look at our performer post, we will see below the content for that post is a list which diplays 10 posts that link to the current post. If we need to remove the post association, all we need to do is edit the event post it is associated to, and remove the post title from the custom field.

Sometimes it is not always appropriate to backlink to the post. In this case we could add a new custom field to the performer post named ‘backlink’ with a value of yes or no. Then put the above code into a conditional statement to decide whether or not to display it, based on the value of the custom field. You can experiment with different possibilities, but this tutorial outlines the general concept and implementation.

If you found this post useful, please leave a comment or you could even digg it!

About Me

David Rivers - Freelance Web Designer I am a stay at home web designer living in Burnham-on-Sea, Somerset. I build websites that are simple, easy to use and conform to W3C standards. I write nice clean code, create elegant design and drink a lot of strong coffee.

Download my vCard
What can I help you with?
  • Custom website design
  • Logo design / branding
  • Design for print
  • Blog design & development
  • E-commerce design & development
  • CMS development / integration
  • Hosting & domain registration
  • Search engine optimisation (SEO)