I am creating some custom meta boxes in my theme to allow user to insert their own custom meta description and keywords overrides (no I will not use a plugin for it). I am having an issue with the posts page as when I'm trying to retrieve the ID for it by using:

$custom_seo_desc = \get_post_meta( $this->post->ID, 'custom_page_desc' );

I keep returning the ID of the first post that appears on the posts page, not the actual ID of the parent page itself. Therefore I cannot seem to set any custom meta data description.

I've done some google searches and I'm stumped for an answer, all I get is articles on how to locate page and posts ID, nothing for a designated posts page.

Does anyone out there have a solution?

  • global $wp_query; $post_id = $wp_query->get_queried_object_id(); try this to find id.
    – BlueSuiter
    Feb 14 '18 at 4:55

You can use get_queried_object_id() to retrieve the page ID.

$custom_seo_desc = \get_post_meta( get_queried_object_id(), 'custom_page_desc' );

You can verify the page for posts ID by looking at the option page_for_posts.

$posts_page_id = get_option( 'page_for_posts' );

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.