Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Sergi Beltran
Sergi Beltran
18,493 Points

List all pages with the custom field 'home' is true

Hi everyone,

I'm trying to display the list of pages with custom field 'home'.

I made the custom field from the plugin 'Advanced Custom Fields'.

If a do this:

wp_list_pages( 'meta_key=home' );

Wordpress display correctly 5 links to pages with the custom field 'home' true.

But I would like to use get_pages() or WP_Query because I would like to get the permalink, the images attached and so on to markup the list as I want.

I'm trying to do this with get_pages():

$args = array(
  'meta_key' => 'home',
  'post_type' => 'page',
  'sort_order' => 'asc'
);
$pages = get_pages($args); 
print_r($pages);

...but the result is a empty array.

I'm trying to do this with WP_Query:

$the_query = new WP_Query( 'meta_key=home' );

if ( $the_query->have_posts() ) {
  while ( $the_query->have_posts() ) {
    echo '<p>Hello</p>'; 
   }
}

...but no paragraphs in the page....nothing.

What can I do?

1 Answer

Did you check their documentation? They give many query examples: http://www.advancedcustomfields.com/resources/query-posts-custom-fields/