Menu

WordPress query pages (not posts) using custom fields

February 1, 2014 - wordpress
<?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.

Leave a Reply