0

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

Totti

0

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 Jun 21 '17 at 10:19
  • yes, you're right, I edited the message to reinforce that message – dbeja Jul 1 '17 at 7:03

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.