get_post_parent() WordPress Function

The get_post_parent() function is used to retrieve the ID of the parent post of the current post.

get_post_parent( int|WP_Post|null $post = null ) #

Retrieves the parent post object for the given post.


Parameters

$post

(int|WP_Post|null)(Optional) Post ID or WP_Post object. Default is global $post.

Default value: null


Top ↑

Return

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


Top ↑

Source

File: wp-includes/post-template.php

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


Top ↑

Changelog

Changelog
VersionDescription
5.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.