WordPress.org

Make WordPress Core

Changeset 51027


Ignore:
Timestamp:
05/26/2021 02:16:01 AM (6 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.

Location:
trunk
Files:
2 edited

Legend:

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

    r50809 r51027  
    764764 * @since 2.0.0
    765765 * @since 5.2.0 Added the `$type` parameter.
     766 * @since 5.8.0 Added the `$status` parameter.
    766767 *
    767768 * @global wpdb $wpdb WordPress database abstraction object.
     
    771772 * @param string $date    Optional post date.
    772773 * @param string $type    Optional post type.
     774 * @param string $status  Optional post status.
    773775 * @return int Post ID if post exists, 0 otherwise.
    774776 */
    775 function post_exists( $title, $content = '', $date = '', $type = '' ) {
     777function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) {
    776778    global $wpdb;
    777779
     
    780782    $post_date    = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
    781783    $post_type    = wp_unslash( sanitize_post_field( 'post_type', $type, 0, 'db' ) );
     784    $post_status  = wp_unslash( sanitize_post_field( 'post_status', $status, 0, 'db' ) );
    782785
    783786    $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
     
    802805        $query .= ' AND post_type = %s';
    803806        $args[] = $post_type;
     807    }
     808
     809    if ( ! empty( $status ) ) {
     810        $query .= ' AND post_status = %s';
     811        $args[] = $post_status;
    804812    }
    805813
  • 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.