WordPress.org

Make WordPress Core

Changeset 50127


Ignore:
Timestamp:
02/01/2021 09:20:44 PM (3 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

Location:
trunk
Files:
2 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}
  • trunk/tests/phpunit/tests/post/template.php

    r49193 r50127  
    453453
    454454    }
     455
     456    /**
     457     * @ticket 33045
     458     */
     459    public function test_get_parent_post() {
     460        $post = array(
     461            'post_status' => 'publish',
     462            'post_type'   => 'page',
     463        );
     464
     465        // Insert two initial posts.
     466        $parent_id = self::factory()->post->create( $post );
     467        $child_id  = self::factory()->post->create( $post );
     468
     469        // Test if child get_parent_post() post returns Null by default.
     470        $parent = get_parent_post( $child_id );
     471        $this->assertNull( $parent );
     472
     473        // Update child post with a parent.
     474        wp_update_post(
     475            array(
     476                'ID'          => $child_id,
     477                'post_parent' => $parent_id,
     478            )
     479        );
     480
     481        // Test if child get_parent_post() post returns the parent object.
     482        $parent = get_parent_post( $child_id );
     483        $this->assertNotNull( $parent );
     484        $this->assertSame( $parent_id, $parent->ID );
     485    }
     486
     487    /**
     488     * @ticket 33045
     489     */
     490    public function test_has_parent_post() {
     491        $post = array(
     492            'post_status' => 'publish',
     493            'post_type'   => 'page',
     494        );
     495
     496        // Insert two initial posts.
     497        $parent_id = self::factory()->post->create( $post );
     498        $child_id  = self::factory()->post->create( $post );
     499
     500        // Test if child has_parent_post() post returns False by default.
     501        $parent = has_parent_post( $child_id );
     502        $this->assertFalse( $parent );
     503
     504        // Update child post with a parent.
     505        wp_update_post(
     506            array(
     507                'ID'          => $child_id,
     508                'post_parent' => $parent_id,
     509            )
     510        );
     511
     512        // Test if child has_parent_post() returns True.
     513        $parent = has_parent_post( $child_id );
     514        $this->assertTrue( $parent );
     515    }
    455516}
Note: See TracChangeset for help on using the changeset viewer.