WordPress query pages (not posts) using custom fields

First off, let me say that I am not a master coder and am still learning. Also, I spent many a precious hour trying to find a solution for this. There are probably many easier/better ways to do it, but this definitely works. If you have found a better way, please post it in the comments section.

The Situation
Now, let me explain what it was that I wanted to do. I had several WordPress pages (not posts) with custom fields already set up. I wanted to have only those pages with a particular custom field name appear on a certain page. So, I wanted to create a custom query that would return only those pages with the custom field name.

The Problem
The problem was that most every query that I tried would not return any of the pages. Each one would only return posts (not pages) with that custom field.

I originally started working with query_posts, but I kept running into a problem with the query returning pages that did not have the custom field and also returning multiple instances of the results (probably an issue with where I had placed the wp_reset_query). I also tried this solution with no luck.

The Solution
Many thanks to Ben at Binary Moon for this article and also to this thread on the WordPress forums that helped me quite a bit.

<?php
$pages = array(
'post_type' => 'page',
'meta_key' => 'your_custom_field_name',
'order' => 'asc'
);
$queryObject = new WP_Query($pages);
?>

<?php if ( $queryObject->have_posts() )
while (
$queryObject->have_posts() ) :
$queryObject->the_post();
?>

<!-- Loop or custom code goes here -->

<?php endwhile; ?>

Be sure to:
1.) Set the custom field on your pages, and publish the pages.
2.) Paste the code above into your loop/page template file/wherever you want the results to appear.
3.) Change the bit with your_custom_field_name to the custom field that you have set up on the pages that you want to query.

For more ways to customize and use the information from the custom fields, check out Part I and Part II at the Perishable Press.

Share

Tags: , , , , , ,

 

 

 


12 Comments for “WordPress query pages (not posts) using custom fields”

  1. Terrific work! This is the type of info that should be shared around the internet. Shame on Google for not positioning this post higher! Come on over and visit my website . Thanks =)

    • Justine says:

      This is cool. I was wondering if this igulpn could be used to automatically convert new custom fields to taxonomies without me having to manually convert them each time.I am using the TDO Forms igulpn and it doesn’t have the option to input custom taxonomies yet but it does have a fairly good custom field option that I can use to collect the taxonomies.

      • Admin says:

        Justine,

        Interesting. I have not taken a look at that plugin, but I will. It sounds like it could be really useful. Thanks.

    • Admin says:

      Chernobyl pictures,

      I posted the steps on how to use this below the code. Which part are you having trouble with?

    • Diop says:

      Thank you thank you thank you.You do a great job explaining how to use this puigln.Better than the puigln website!I have run into two issues in working with images.No idea if you can help, but figured I might post my experience.PATHSlocally: get_image($whatever) returns paths with the path separators in the wrong position for windows. \ versus /Luckily it works on my *nix Server. MULTIPLE IMAGESIn the admin screen I see 4 images.Your example was very helpful to set up a loop.Unfortunately image1 is okay, image2 is okay, then the rest are all copies of 2!I really like the interface but I need to figure this bug out.I deeply appreciate your blog post!

  2. good idea im gonna try it

    • Cami says:

      Good stuff. We’re moving our sites to rniocporate Flutter as well since it is a more organic model for content entry using WordPress. What we used to do, however, is to assign Categories for items like News and events, and just train the client to enter content and check the required category. Using a combination of query post and conditional statements, the required info would be displayed ont he front-end and everybody was happy. With teh advent of more complex sites, and of course, the need to just make things easier for the client, we are overhauling things to rniocporate Flutter. Good tut, mate.

      • Admin says:

        Cami,

        I haven’t heard of Flutter, but I will have to check it out. I agree with you whole-heartedly that custom fields in WordPress get the job done, but are not the prettiest. It is also possible to modify the admin panel to integrate custom fields in a prettier way, much like you see with some plugins that add boxes to right of the text editor on for posts and pages.

        Thanks for sharing.

  3. Thank you for the posst. I really enjoyed reading it!

  4. Good post. You made some great points.

Leave a Comment