I'm customizing a page template and having an issue with getting a page id from a custom field.

The value of the custom field contains the page ID whose contents are to be called.

For a normal page I would do it like this:

<?php query_posts('page_id=155'); global $more; $more = 1;  ?>

But how do I get the value of the custom field as page ID for the query?

Thanks in advance


1 Answer 1


You get a custom field by using get_post_meta:

<?php $pageID = get_post_meta( get_the_ID(), 'custom_field_name', true ); ?>

You can then use that for the query you need, but you should not use query_posts, this function overrides the main query and isn't normally used on themes or plugins, it should be avoided. This can increase page loading times and it's generally better to use WP_Query, get_posts or pre_get_posts action (in case you want to change the main query).

You can also use get_post if you just need to get the page data:

<?php $page = get_post( $pageID ); ?>
  • 1
    Never use query_posts(), it is very bad, even worst in a template file. Use WP_Query instead.
    – cybmeta
    Commented Jun 21, 2017 at 10:19
  • yes, you're right, I edited the message to reinforce that message
    – dbeja
    Commented Jul 1, 2017 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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