WordPress.org

Make WordPress Core


Ignore:
Timestamp:
05/26/2021 02:16:01 AM (8 months ago)
Author:
peterwilsoncc
Message:

Posts, Post Types: Improve post_exists() query.

Add $status parameter to post_exists() to allow developers to specify a post type, date and status to ensure they hit the wp_posts table's type_status_date index when determining if a post exists.

Props apokalyptik, boonebgorges, brettshumaker, DrewAPicture, MikeHansenMe, peterwilsoncc, whyisjake.
Fixes #34012.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/includesPost.php

    r50527 r51027  
    904904        $this->assertSame( 0, post_exists( $title, null, null, 'post' ) );
    905905    }
     906
     907    /**
     908     * Test the status support in post_exists()
     909     *
     910     * @ticket 34012
     911     */
     912    public function test_post_exists_should_support_post_status() {
     913        $title       = 'Foo Bar';
     914        $post_type   = 'post';
     915        $post_status = 'publish';
     916        $post_id     = self::factory()->post->create(
     917            array(
     918                'post_title'  => $title,
     919                'post_type'   => $post_type,
     920                'post_status' => $post_status,
     921            )
     922        );
     923        $this->assertSame( $post_id, post_exists( $title, null, null, null, $post_status ) );
     924    }
     925
     926
     927    /**
     928     * Test the type and status query in post_exists()
     929     *
     930     * @ticket 34012
     931     */
     932    public function test_post_exists_should_support_post_type_status_combined() {
     933        $title       = 'Foo Bar';
     934        $post_type   = 'post';
     935        $post_status = 'publish';
     936        $post_id     = self::factory()->post->create(
     937            array(
     938                'post_title'  => $title,
     939                'post_type'   => $post_type,
     940                'post_status' => $post_status,
     941            )
     942        );
     943        $this->assertSame( $post_id, post_exists( $title, null, null, $post_type, $post_status ) );
     944    }
     945
     946    /**
     947     * Test that post_exists() doesn't find an existing draft post when looking for publish
     948     *
     949     * @ticket 34012
     950     */
     951    public function test_post_exists_should_only_match_correct_post_status() {
     952        $title       = 'Foo Bar';
     953        $post_type   = 'post';
     954        $post_status = 'draft';
     955        $post_id     = self::factory()->post->create(
     956            array(
     957                'post_title'  => $title,
     958                'post_type'   => $post_type,
     959                'post_status' => $post_status,
     960            )
     961        );
     962        $this->assertSame( 0, post_exists( $title, null, null, null, 'publish' ) );
     963    }
     964
     965    /**
     966     * Test the status support in post_exists()
     967     *
     968     * @ticket 34012
     969     */
     970    public function test_post_exists_should_not_match_invalid_post_type_and_status_combined() {
     971        $title       = 'Foo Bar';
     972        $post_type   = 'post';
     973        $post_status = 'publish';
     974        $post_id     = self::factory()->post->create(
     975            array(
     976                'post_title'  => $title,
     977                'post_type'   => $post_type,
     978                'post_status' => $post_status,
     979            )
     980        );
     981
     982        $this->assertSame( 0, post_exists( $title, null, null, $post_type, 'draft' ) );
     983        $this->assertSame( 0, post_exists( $title, null, null, 'wp_tests', $post_status ) );
     984    }
    906985}
Note: See TracChangeset for help on using the changeset viewer.