Forums
Home / Themes and Templates / Get pages with meta_key & meta_value
(@martinlucas)
13 years, 2 months ago
I’ve grabbed the code from this previous support post; http://wordpress.org/support/topic/use-the-post-image-to-wp_list_pages?replies=9#post-1386992 and tweaked it ever so slightly to only show the pages where a custom field equals a certain value, but it doesn’t work.
I’ve been round and round and simplified the code as much as possible – but still nothing. Can anyone tell me if there’s something wrong with the get_pages code here, it works fine if I pull in pages without mentioning the meta_key / value.
<?php $pages = get_pages('meta_key=productfeatured&meta_value=yes'); $output = ''; foreach($pages as $value){ $thumb = get_the_post_thumbnail( $value->ID, array(175,175), $attr = '' ); $output .= "<li>"; $output .= "<a href='" . $value->post_name . "' >" . $thumb . "</a><br />"; $output .= "<a href='" . $value->post_name . "' >" . $value->post_title . "</a>"; $output .= "</li>"; } echo $output; ?>
(@keesiemeijer)
try it with get posts:
$pages = get_posts('meta_key=productfeatured&meta_value=yes&post_type=page');
To add more confusion, this code — replacing the above code — works fine.
<?php wp_list_pages('title_li=&meta_key=productfeatured&meta_value=yes&number=3'); ?>
Thanks keesiemeijer – that worked perfectly, thanks so much!