How to list posts with a specific custom field

Custom fields are a great way of extending your posts’ functionality. Instead of letting users deal with the code, you can let them enter a value in a custom field and do something with that information – there are endless possibilities.

Sometimes, when you are already using specific custom fields, you may need to list posts with those fields.

For example, if you are writing game reviews and already have different custom fields where you display publisher, genre, score, etc. in a separate information box in your review, you might want to display only games from that specific publisher, games from FPS genre or only those reviews with a score of 10.

This kind of information could really be interesting to your visitor. You can allow them to browse your game reviews in much more details or allow them to see top 10 rated games on your site. Even if you don’t want to publish a new list, this method can help you in data mining – instead of creating an article from it, you can list all posts with certain property for research purposes only.

In our opinion, the best way of listing posts sorted by a custom field is by creating a new page template and showing a list on a new page. This method allows you to organize your pages and to easily use a new page for displaying what you need. In the following lines, we are about to show you how to find all posts with a specific custom field and/or its value and show it in a new page.

Create a custom template:

Let’s stay with the game review example where we want to display all games from Bethesda Softworks (did anyone say Fallout?). For starters, you will need to create and prepare a custom template:

  1. Create a custom page template and name it bethesda-games.php
  2. Copy and paste the following code into the file:
  3. 'publisher’, 'meta_value' => 'Bethesda' )); if ( have_posts() ) while ( have_posts() ) : the_post(); echo '
  4. '; the_title(); echo '
  5. '; endwhile; wp_reset_query(); ?>
  6. Save changes

The code is ready to go through all custom fields and to find only ‘Publisher’ keys with the value of ‘Bethesda’. You can change key and the value to anything you want. If you would like to display all posts with ‘Publisher’ custom field containing any value, simply remove ‘meta_value’ row entirely.

Don’t forget to change the title of the template to something you will recognize later.

Add the page:

Now your template is ready so you can continue creating the list:

  1. Navigate to Pages->Add New
  2. Add a title
  3. On the right side, find “Page Attributes”
  4. Select your template from the “Template” dropdown list
  5. Publish the page

That’s it! Now if you open your new page, you can see the list of all your posts which contain custom fields with the values you have chosen.

Now you have to add some text on your new page, style the list and find the best way to show it to your visitors. We hope that this method will help you sort your posts in a whole other level and that you will enjoy it.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

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