has_post_parent()WP 5.7.0

Checks if there is a parent post for the specified or current post in the loop.

When you need to get the parent post, use get_post_parent().

No Hooks.

Returns

true|false. Returns true, if there is a parent post and false, if not.

Usage

has_post_parent( $post );
$post(integer/WP_Post/null)
ID of the post or its object for which to check for a parent post.
Default: global $post

Examples

0

#1 Check the availability of the parent post

$post_id = 5;

if ( has_post_parent( $post_id ) ) {
	echo 'The specified post has a parent post';
} 
else {
	echo 'The specified post does not have a parent post';
}

Changelog

Since 5.7.0 Introduced.

has_post_parent() code WP 6.8.3

function has_post_parent( $post = null ) {
	return (bool) get_post_parent( $post );
}