0

I'm wondering how to get at custom field information attached to a page, not a post.

Using get_post_meta seems like the right idea, but I don't know how to tell the function to look at page ids and not post ids. Also it's no clear to me if this function can work outside the loop.

A short piece of code showing how to access the page custom field would be really useful.

0

Sometimes WordPress is criticized treating everything as a post. The matter of fact is, post type pages, post type post - both are actually Post in database. So no post will collide with any page ID. :)

So simply a get_post_meta() is enough.

But if you still want something specific to Pages, you can use:

if( is_page() ) get_post_meta(...);

Yes, you can use get_post_meta() outside the loop. But instead of passing the post_id using get_the_ID() you have to pass the post_id manually.

1

Pages, Posts, and Custom Post Types are all stored in the same table with unique IDs, and in the case of meta data it works the same way for all types.

In the loop you can use:

echo get_post_meta( get_the_ID(), 'your_key', true );

Or anywhere on a page, get_queried_object_id() will give you the page ID:

echo get_post_meta( get_queried_object_id(), 'your_key', true );

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.