In this 2 part tutorial I will explain how to insert content from another post, into our new post, using only custom fields. I recently came across a problem in WordPress to link posts together. The idea was to use WordPress more as a CMS than a blogging platform. As many of you know, WordPress is extremely flexible in that it gives the designer many ways to display content. I decided to explore some of these ways to query posts, and link them together, saving time publishing content
Why are we linking posts together?
This technique is great for publishing content. The problem I came across was sometimes in WordPress you need to include content from another post in your new post. I searched the WordPress forums for a solution to this, and this article explains the same problem I had. Their example uses Whiskey malts, and the distillery they came from. The solution featured on their post, however, only displayed custom fields on the post, and you could not call template tags like you would in the loop. I needed to create events, and display all of the performers for the event, as well as venue information, including the_content() and the_tags(). To make the site as dynamic as possible, I decided it would be a good idea to create individual posts for all performers & venues, and pull it all together using an events post.
1. Requirements
- WordPress 2.7.1 (not tested in earlier versions)
- A basic understanding of PHP
- An understanding of the WordPress codex
2. Setup WordPress categories & posts
First we need to add our categories. I added three for my project, but in this example we’ll just use two.
The Categories we’ll use here are:
- Performers
- Events
Now let’s create some sample posts. First we’ll add some performers. I’m adding a post titled ‘Joe Bloggs’, with a biography about the performer in the write panel, and some random custom fields such as performer website and email address. Let’s publish this post in the ‘performers’ category and move onto creating our event post.
The event post should contain a post title (I’m calling mine ‘Gig at the Old Spice Bar’), some content about the event, and published in the events category. We’ll also add some custom fields to this post. Add a new custom field name ‘performer’ and value ‘Joe Bloggs’ (or the post name we want to add content from), and do this for each post we want to display in our event post. It is important to mention here that the value of the custom field must be an exact match to the post name we want to insert content from.
UPDATE! Above it states ‘the value of the custom field must be an exact match to the post name we want to insert content from’. This statement is incorrect. The value of the custom field should be an exact match of the post slug. In my example, where I have inserted Joe Bloggs, the slug is joe-bloggs, so the custom field can read any of these: ‘joe bloggs’, ‘Joe Bloggs’ or ‘joe-bloggs’. However if you change the post slug, you will need to update this custom field as well. Any changes to the post name will not affect this technique.
3. Create new single.php for each category
In this example I am using WordPress default ‘kubrick’ theme. Go to your favourite editor, and duplicate single.php from within your theme folder. Rename this to ‘event-single.php’. Duplicate again but rename this copy to ‘performer-single.php’. Duplicate this a third time and rename to ‘default-single.php’.
Open single.php and replace it’s content with the following:
<?php
if ( in_category('performers')){ include('performer-single.php');
} elseif ( in_category('events')) { include('event-single.php');
} else { include('default-single.php'); }
?>
Now that we have added these files, our posts will be displayed in different template files, depending on what category the post is in.
4. Edit event-single.php
Let’s add some magic to event-single.php to enable us to show some content from the post ‘Joe Bloggs’.
Open event-single.php which we created earlier, and find a suitable place to add the code below. This is where we will display the content from our other posts. I have started my code on line 70, after <?php endif; ?>.
Rewind posts
<?php rewind_posts(); ?>
Get the values of custom field name ‘performer’ and store it as array $performers;
<?php $performers = get_post_meta($post->ID, 'performer', false); ?>
Let’s loop through all of our performers using PHP foreach
<?php foreach($performers as $performer) { ?>
Create a new WordPress query, which queries only posts in ‘performers’ category, and with a post name of that from the custom field and returns only 1 post.
<?php $get_performer = new WP_Query('category_name=performers&post_type=post&name=' . $performer . '&numberposts=1'); ?>
We need this line to be able to use template tags inside our loop such as the_tags();
<?php $wp_query->in_the_loop = true; ?>
Lets start our WordPress loop
<?php while ($get_performer->have_posts()) : $get_performer->the_post(); $do_not_duplicate = $post->ID; ?>
Now add the content we want to display using template tags
<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 />
end our WordPress loop
<?php endwhile; ?>
end our performers foreach
<?php } ?>
What we have essentially done in the code above is create a new WP_Query for each of our performers listed in the custom fields.
5. View our site for changes
Now let’s take a look at our WordPress site. If we look at a post in the performers category, nothing has changed. However, if we look at a post in the events category (my post is ‘Gig at the Old Spice Bar’), we should see our performers included in there as well.
In part 2 of this tutorial, I will explain how to add backwards compatibility to your performer post, without adding any more custom fields to WordPress and using only the performer-single.php file. See you there!
I am a stay at home web designer living in
I just made a WordPress Mass Custom Fields Manager Plugin, you can get it on my blog. Please leave a feedback if you use it.
Great job! Can’t wait for the second part.
Thanks Brian. You can now catch part 2 of this tutorial here: http://www.jadedstudio.com/tutorials/linking-wordpress-posts-using-custom-fields-part-2
Good work! Thank you very much! I always wanted to write in my blog something like that. Can I take part of your post to my blog? Of course, I will add backlink?
You can use any part of this post on your website if you find it useful, all I ask is for you to credit me with a link to the original post!
da best. Keep it going! Thank you