Make WordPress Core


Ignore:
Timestamp:
03/21/2019 12:43:48 PM (7 years ago)
Author:
desrosj
Message:

Posts, Post Types: Add type parameter to post_exists().

This allows post exists checks scoped to a specific post type.

Props sgarza, birgire, swissspidy.
Fixes #37406.

File:
1 edited

Legend:

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

    r44938 r44959  
    732732
    733733/**
    734  * Determine if a post exists based on title, content, and date
     734 * Determines if a post exists based on title, content, date and type.
    735735 *
    736736 * @since 2.0.0
     737 * @since 5.2.0 Added the `$type` parameter.
    737738 *
    738739 * @global wpdb $wpdb WordPress database abstraction object.
    739740 *
    740  * @param string $title Post title
    741  * @param string $content Optional post content
    742  * @param string $date Optional post date
     741 * @param string $title   Post title.
     742 * @param string $content Optional post content.
     743 * @param string $date    Optional post date.
     744 * @param string $type    Optional post type.
    743745 * @return int Post ID if post exists, 0 otherwise.
    744746 */
    745 function post_exists( $title, $content = '', $date = '' ) {
     747function post_exists( $title, $content = '', $date = '', $type = '' ) {
    746748        global $wpdb;
    747749
     
    749751        $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
    750752        $post_date    = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
     753        $post_type    = wp_unslash( sanitize_post_field( 'post_type', $type, 0, 'db' ) );
    751754
    752755        $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
     
    766769                $query .= ' AND post_content = %s';
    767770                $args[] = $post_content;
     771        }
     772
     773        if ( ! empty( $type ) ) {
     774                $query .= ' AND post_type = %s';
     775                $args[] = $post_type;
    768776        }
    769777
Note: See TracChangeset for help on using the changeset viewer.