Introducing new Post Parent related functions in WordPress 5.7

Update: These functions were originally introduced as get_parent_post() and has_parent_post() but have since been renamed to get_post_parent() and has_post_parent().

In WordPress 5.7, two long awaited functions were added to determine if a post has a parent and to get the related parent Post object. These functions are quite simple to use. They reduce the logic needed in themes and plugins.

Before committing this change the pluginPlugin A plugin is a piece of software containing a group of functions that can be added to a WordPress website. They can extend functionality or add new features to your WordPress websites. WordPress plugins are written in the PHP programming language and integrate seamlessly with WordPress. These can be free in the WordPress.org Plugin Directory https://wordpress.org/plugins/ or can be cost-based plugin from a third-party repository was checked for existing plugins and themes that already use either of the two functions. No plugin or theme is affected in the WordPress.orgWordPress.org The community site where WordPress code is created and shared by the users. This is where you can download the source code for WordPress core, plugins and themes as well as the central location for community conversations and organization. https://wordpress.org/ repository.

Kind reminder for WordPress developers: if you use the get_post_parent() or has_post_parent() functions in any custom plugin or theme, please prefix them ahead of WP 5.7 to avoid any PHPPHP The web scripting language in which WordPress is primarily architected. WordPress requires PHP 5.6.20 or higher error.

For reference, this enhancementenhancement Enhancements are simple improvements to WordPress, such as the addition of a hook, a new feature, or an improvement to an existing feature. has been addressed in ticketticket Created for both bug reports and feature development on the bug tracker. #33045.

New template tagtag A directory in Subversion. WordPress uses tags to store a single snapshot of a version (3.6, 3.6.1, etc.), the common convention of tags in version control systems. (Not to be confused with post tags.): get_post_parent()

This new template tag accepts one parameter: a post ID or a WP_Post object. By default it uses the global variable $post if available. It retrieves the parent WP_Post object for the given post.

New conditional tag: has_post_parent()

This new conditional tag accepts one parameter: a post ID or a WP_Post object. By default it uses global variable $post if available. It returns true if the post has a parent, false otherwise.

Basic example of use

These two functions can be combined to display a “Back to parent page” link:

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

Props @chaton666 and @johnbillion for proofreading this dev notedev note Each important change in WordPress Core is documented in a developers note, (usually called dev note). Good dev notes generally include a description of the change, the decision that led to this change, and a description of how developers are supposed to work with that change. Dev notes are published on Make/Core blog during the beta phase of WordPress release cycle. Publishing dev notes is particularly important when plugin/theme authors and WordPress developers need to be aware of those changes.In general, all dev notes are compiled into a Field Guide at the beginning of the release candidate phase..

#5-7, #dev-notes