How to get the post's parent ID?

I have a template which outputs the blog posts (Blog Page), I open one of the posts which use single.php template, Now how can I get the ID of it's parent which is the (Blog Page)?

I should say that I want to use this ID to get the meta box value of the blog page in single.php.

The same goes for a custom post type, let's say 'project', Where a page template list the project's posts (Projects Page) and single-project.php for the single project post. in this case I want to get the (Projects Page) ID in single-project.php.

Topic id blog posts Wordpress

Category Web


WordPress 5.7 introduces a new helper function to more easily fetch the parent post's ID: get_post_parent()

This can also be used in conjunction with has_post_parent(), so you could have something like looks like:

<?php if ( has_post_parent() ) : ?>
    <a href="<?php the_permalink( get_post_parent() ); ?>">
        <?php
        echo sprintf(
            esc_html__( 'Back to parent page: %s', 'text-domain' ),
            get_the_title( get_post_parent() )
        );
        ?>
    </a>
<?php endif; ?>

Note that these functions accept a "child post ID" as a parameter, which defaults to the current post.

https://make.wordpress.org/core/2021/02/10/introducing-new-post-parent-related-functions-in-wordpress-5-7/


Use $post->post_parent to get the parent ID of the post. Here $post is an object with properties.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.