The get_post_parent() WordPress PHP function retrieves the parent post object for the given post.
Usage
get_post_parent( $post );
Custom example:
$parent_post = get_post_parent( 42 ); echo 'The parent post title is: ' . $parent_post->post_title;
Parameters
$post
int|WP_Post|null (Optional) – Post ID or WP_Post object. Default is global$post
. Default: null
More information
See WordPress Developer Resources: get_post_parent()
Examples
Display parent post title
Display the title of the parent post.
$parent_post = get_post_parent(); if ( $parent_post ) { echo 'Parent post title: ' . $parent_post->post_title; }
Display parent post author
Display the author of the parent post.
$parent_post = get_post_parent(); if ( $parent_post ) { $parent_author_id = $parent_post->post_author; $parent_author = get_the_author_meta( 'display_name', $parent_author_id ); echo 'Parent post author: ' . $parent_author; }
Display parent post permalink
Display the permalink of the parent post.
$parent_post = get_post_parent(); if ( $parent_post ) { $parent_permalink = get_permalink( $parent_post->ID ); echo 'Parent post permalink: ' . $parent_permalink; }
Display parent post categories
Display the categories of the parent post.
$parent_post = get_post_parent(); if ( $parent_post ) { $parent_categories = get_the_category( $parent_post->ID ); $parent_categories_list = wp_list_pluck( $parent_categories, 'name' ); echo 'Parent post categories: ' . implode( ', ', $parent_categories_list ); }
Check if parent post has a specific tag
Check if the parent post has a specific tag, such as “featured”.
$parent_post = get_post_parent(); $featured_tag = 'featured'; if ( $parent_post && has_tag( $featured_tag, $parent_post->ID ) ) { echo 'The parent post has the "' . $featured_tag . '" tag.'; } else { echo 'The parent post does not have the "' . $featured_tag . '" tag.'; }