Build 10 games and learn game development along the way in this fast paced game development course

Get the source code of 12 commercial games, loaded 50+ million times, and learn the secrets of game design

Learn how to make a successful Flash game from a real world example. Fully commented source code

Create realistic Flash physics games from scratch with advanced features, from Angry Birds to Crush the Castle

How to list all WordPress posts with the same custom field value

When I celebrated my four years of blogging I also introduced a big problem this blog is facing: there is so much content it’s not easy to find what you are looking for.

I received some interesting suggestions, and one of them is to link all posts of the same series. This is what a reader said: “In the first post, put a link of all the steps (requires manual editing)”.

I found a way to put an index on each post of a series using custom fields, and I am sure you’ll find it interesting. Let’s take Create a Flash game like Rebuild Chile series as example.

I want, in every post of the series, to show the full list of posts of the series. Without a lot of manual editing, of course.

So, that’s what I did: first, for every post of the series, I added a custom field called series with Create a Flash game like Rebuild Chile value. The idea is to add a series custom field to every post in every series with a proper value.

If you don’t know how to add a custom field, here it is a little help:

At the end of the Post page admin, you’ll find the form to add new custom fields. That’s how I added series custom field. Now, a few lines of code to rule them all… this is what you have to include somewhere inside the so-called WordPress Loop:

Line 1: Assigning to a variable called $series the value of the series custom field value, if any. Then, if there is a value…

Line 5: Creating a $chapters array and populating it with every post (numberpost=-1) whose series custom field (meta_key=series) has $series value (meta_value=$series) ordering them by date (orderby=date) in ascending way (order=ASC). Then scanning the entire array, saving each item into $chapter
variable.

Line 6: Adding the post title, along with a link if it’s not the current post

The remaining lines are just HTML to let me style the content.

You can see the result in the Create a Flash game like Rebuild Chile series… I have to find the right style but I think I’m on the right way.

Do you like this way to use custom fields?

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
Loading ... Loading ...
Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 5 comments

  1. ColinDiam

    on May 29, 2010 at 1:36 pm

    A Very ingenious idea could see that being very helpful especially how it is so simple to implement

  2. Pipo

    on May 30, 2010 at 2:53 pm

    Yeah, finally!
    Thanks for your blog. I missed this feature so much at the “tile based platform engine-tutorial”.
    Thanks again.

  3. Davide

    on November 24, 2011 at 12:31 pm

    Thanks for this tutorial, very useful.
    How should I modify your code to run this query for custom post type instead of normal posts?

  4. Davide

    on November 24, 2011 at 1:27 pm

    Sorry for the double post:
    I’ve resolved by adding
    post_type=myposttype
    in the get_posts(”) query.
    Now it’s perfect, thanks to the possibility from your code to differentiate the current post from the others with the same custom fields.
    Thanks again Emanuele!

  5. Marzia Agnetti

    on March 19, 2012 at 12:53 am

    Thank you for this code, I’ve been searching something like this for a long time! But now I really need to find a way to display the featured image beside the post title. Is that possible? Sorry I’m not expert in php. Please help!

Thank you for the download!!

I hope you will find it useful and make something interesting out of it

To keep up to date with the blog, why don't you like my Facebook page and follow me on Twitter?

Want to learn more? Don't miss this:

Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now

×