0

Very simply, get_pages() will return all pages.

I want to get all pages that have a custom field key called 'basePrice', regardless of the value.

But when I try to use

get_pages(array(
    'meta_key' => 'basePrice'
))

The array is always empty, and no pages are returned.

I know I can create a custom WP query, but I Don't want to, I want to keep this simple, but I also want to be able to output a hierarchical list of children pages nested under the parents when I use a foreach loop.

If I use get_posts, the meta_key works, but all pages (including children) are sorted into an alphabetical list, not relating to their parents.

  • is the meta_key the only argument you are passing? – hwl Jul 31 '17 at 17:45
  • Yes, it's the only custom arugment I want to pass. – Lee Aug 4 '17 at 12:08
0

I'm not sure if this is still true, but i tried this one time and found a similar question:

Understand this is not my answer, but what i found that ended up working. It was a year ago, and I haven't tried if querying by meta_key works yet... From this question:

https://stackoverflow.com/questions/32104826/wordpress-get-pages-meta-key-dont-show-page

This is the answer that was provided:

As of now Wordpress documentation for get_pages() says the function doesn't support querying with a meta_key:

NOTE: This function will currently not retrieve pages using the 'meta_key' and 'meta_value' parameters in the $args array as described below. Until this is fixed in the WordPress codebase, you may want to use get_posts() instead.

So your code should call get_posts() instead:

$pages_args = array(
  'orderby' => 'menu_order',
  'parent' => 0,
  'post_type' => 'page',
  'post_status' => 'publish',
  'meta_key'  => 'dont_show_in_nav',
  'meta_value' => 'true'
);
$pages = get_posts($pages_args);

Note that I quoted 'true', since to Wordpress it's just a text field. Also, some arguments for get_posts are slightly different than for get_pages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.