mirror of
git://develop.git.wordpress.org/
synced 2025-04-21 04:31:55 +02:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@51027 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
852a2c216c
commit
55db8c73a3
@ -763,6 +763,7 @@ function get_default_post_to_edit( $post_type = 'post', $create_in_db = false )
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @since 5.2.0 Added the `$type` parameter.
|
||||
* @since 5.8.0 Added the `$status` parameter.
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*
|
||||
@ -770,15 +771,17 @@ function get_default_post_to_edit( $post_type = 'post', $create_in_db = false )
|
||||
* @param string $content Optional post content.
|
||||
* @param string $date Optional post date.
|
||||
* @param string $type Optional post type.
|
||||
* @param string $status Optional post status.
|
||||
* @return int Post ID if post exists, 0 otherwise.
|
||||
*/
|
||||
function post_exists( $title, $content = '', $date = '', $type = '' ) {
|
||||
function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) {
|
||||
global $wpdb;
|
||||
|
||||
$post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
|
||||
$post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
|
||||
$post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
|
||||
$post_type = wp_unslash( sanitize_post_field( 'post_type', $type, 0, 'db' ) );
|
||||
$post_status = wp_unslash( sanitize_post_field( 'post_status', $status, 0, 'db' ) );
|
||||
|
||||
$query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
|
||||
$args = array();
|
||||
@ -803,6 +806,11 @@ function post_exists( $title, $content = '', $date = '', $type = '' ) {
|
||||
$args[] = $post_type;
|
||||
}
|
||||
|
||||
if ( ! empty( $status ) ) {
|
||||
$query .= ' AND post_status = %s';
|
||||
$args[] = $post_status;
|
||||
}
|
||||
|
||||
if ( ! empty( $args ) ) {
|
||||
return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) );
|
||||
}
|
||||
|
@ -903,4 +903,83 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
|
||||
);
|
||||
$this->assertSame( 0, post_exists( $title, null, null, 'post' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the status support in post_exists()
|
||||
*
|
||||
* @ticket 34012
|
||||
*/
|
||||
public function test_post_exists_should_support_post_status() {
|
||||
$title = 'Foo Bar';
|
||||
$post_type = 'post';
|
||||
$post_status = 'publish';
|
||||
$post_id = self::factory()->post->create(
|
||||
array(
|
||||
'post_title' => $title,
|
||||
'post_type' => $post_type,
|
||||
'post_status' => $post_status,
|
||||
)
|
||||
);
|
||||
$this->assertSame( $post_id, post_exists( $title, null, null, null, $post_status ) );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test the type and status query in post_exists()
|
||||
*
|
||||
* @ticket 34012
|
||||
*/
|
||||
public function test_post_exists_should_support_post_type_status_combined() {
|
||||
$title = 'Foo Bar';
|
||||
$post_type = 'post';
|
||||
$post_status = 'publish';
|
||||
$post_id = self::factory()->post->create(
|
||||
array(
|
||||
'post_title' => $title,
|
||||
'post_type' => $post_type,
|
||||
'post_status' => $post_status,
|
||||
)
|
||||
);
|
||||
$this->assertSame( $post_id, post_exists( $title, null, null, $post_type, $post_status ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that post_exists() doesn't find an existing draft post when looking for publish
|
||||
*
|
||||
* @ticket 34012
|
||||
*/
|
||||
public function test_post_exists_should_only_match_correct_post_status() {
|
||||
$title = 'Foo Bar';
|
||||
$post_type = 'post';
|
||||
$post_status = 'draft';
|
||||
$post_id = self::factory()->post->create(
|
||||
array(
|
||||
'post_title' => $title,
|
||||
'post_type' => $post_type,
|
||||
'post_status' => $post_status,
|
||||
)
|
||||
);
|
||||
$this->assertSame( 0, post_exists( $title, null, null, null, 'publish' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the status support in post_exists()
|
||||
*
|
||||
* @ticket 34012
|
||||
*/
|
||||
public function test_post_exists_should_not_match_invalid_post_type_and_status_combined() {
|
||||
$title = 'Foo Bar';
|
||||
$post_type = 'post';
|
||||
$post_status = 'publish';
|
||||
$post_id = self::factory()->post->create(
|
||||
array(
|
||||
'post_title' => $title,
|
||||
'post_type' => $post_type,
|
||||
'post_status' => $post_status,
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertSame( 0, post_exists( $title, null, null, $post_type, 'draft' ) );
|
||||
$this->assertSame( 0, post_exists( $title, null, null, 'wp_tests', $post_status ) );
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user