get_post_parent()WP 5.7.0

Retrieves the parent post object for the given post.

No Hooks.

Return

WP_Post|null. Parent post object, or null if there isn't one.

Usage

get_post_parent( $post );
$post(int|WP_Post|null)
Post ID or WP_Post object.
Default: global $post

Examples

0

#1 Display the title of the parent post

$child_post_id = 5;
$parent_post = get_post_parent( $post );

if ( $parent_post ) {
	echo 'The title of the parent post: ' . get_the_title( $parent_post );
}
else {
	echo 'The specified post does not have a parent post;
}

Changelog

Since 5.7.0 Introduced.

get_post_parent() code WP 6.5.3

function get_post_parent( $post = null ) {
	$wp_post = get_post( $post );
	return ! empty( $wp_post->post_parent ) ? get_post( $wp_post->post_parent ) : null;
}