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

Emanuele Feronato Php, WordPress

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

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?

From null to full HTML5 cross platform game

I will take you by hand from the bare bones of JavaScript programming through the creation of a full cross platform HTML5 game, with detailed explainations and source code.

If you don't know where to start, then From null to full HTML5 cross platform game is the book for you.

Comments 5

  1. Pipo

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

  2. Davide

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

  3. Davide

    Sorry for the double post:
    I’ve resolved by adding
    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!

  4. Marzia Agnetti

    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!

Leave a Reply

Your email address will not be published. Required fields are marked *