WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/01/2021 09:20:44 PM (5 months ago)
Author:
johnbillion
Message:

Posts, Post Types: Introduce new functions for determining if a post has a parent (has_post_parent()) and to fetch the post parent (get_post_parent()).

These functions are simple but reduce the logic needed in themes and plugins.

Props ramiy, sebastian.pisula, birgire, audrasjb, xkon

Fixes #33045

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post-template.php

    r49977 r50127  
    19641964    echo '</ul>';
    19651965}
     1966
     1967/**
     1968 * Retrieves the parent post object for the given post.
     1969 *
     1970 * @since 5.7.0
     1971 *
     1972 * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global $post.
     1973 * @return WP_Post|null Parent post object, or null if there isn't one.
     1974 */
     1975function get_parent_post( $post = null ) {
     1976    $wp_post = get_post( $post );
     1977    return ! empty( $wp_post->post_parent ) ? get_post( $wp_post->post_parent ) : null;
     1978}
     1979
     1980/**
     1981 * Returns whether the given post has a parent post.
     1982 *
     1983 * @since 5.7.0
     1984 *
     1985 * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global $post.
     1986 * @return bool Whether the post has a parent post.
     1987 */
     1988function has_parent_post( $post = null ) {
     1989    return (bool) get_parent_post( $post );
     1990}
Note: See TracChangeset for help on using the changeset viewer.