Make WordPress Core


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