Make WordPress Core

Ticket #34012: post.diff

File post.diff, 2.2 KB (added by apokalyptik, 9 years ago)

fix for post_exists() query performance

  • post.php

     
    610610/**
    611611 * Determine if a post exists based on title, content, and date
    612612 *
     613 * For best performance use the $date, $type, and $status together
     614 * all of which are required to properly use a compound index.
     615 * $date is left where it is positionally for legacy reasons
     616 *
    613617 * @since 2.0.0
     618 * @since 4.4.0 Added the $type, $status parameters
    614619 *
    615620 * @global wpdb $wpdb
    616621 *
     
    617622 * @param string $title Post title
    618623 * @param string $content Optional post content
    619624 * @param string $date Optional post date
     625 * @param string $type Optional post type
     626 * @param string $status Optional post status
    620627 * @return int Post ID if post exists, 0 otherwise.
    621628 */
    622 function post_exists($title, $content = '', $date = '') {
     629function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) {
    623630        global $wpdb;
    624631
    625         $post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
    626         $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
    627         $post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
    628 
    629         $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
    630         $args = array();
    631 
    632         if ( !empty ( $date ) ) {
    633                 $query .= ' AND post_date = %s';
    634                 $args[] = $post_date;
     632        $query = array();
     633        foreach( compact( 'title', 'content', 'date', 'type', 'status' ) as $key => $val ) {
     634                if ( empty( $val ) ) {
     635                        continue;
     636                }
     637                $key = sprintf( 'post_%s', $key );
     638                $query[] = $wpdb->prepare(
     639                        $key . ' = %s',
     640                        wp_unslash( sanitize_post_field( $key, $val, 0, 'db' ) )
     641                );
    635642        }
    636643
    637         if ( !empty ( $title ) ) {
    638                 $query .= ' AND post_title = %s';
    639                 $args[] = $post_title;
     644        if ( empty( $query ) ) {
     645                return 0;
    640646        }
    641647
    642         if ( !empty ( $content ) ) {
    643                 $query .= 'AND post_content = %s';
    644                 $args[] = $post_content;
    645         }
    646 
    647         if ( !empty ( $args ) )
    648                 return (int) $wpdb->get_var( $wpdb->prepare($query, $args) );
    649 
    650         return 0;
     648        return (int)$wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE 1=1 AND " . implode( ' AND ', $query ) . ' LIMIT 1' );
    651649}
    652650
    653651/**