WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/05/2016 07:50:02 PM (5 years ago)
Author:
joemcgill
Message:

Media: Better handling of JOINs when searching filenames.

Following [38625], any media searches that already included JOINs,
e.g., tax_queries, would get trampled when we joined the post meta
table to search for filenames. This preserves existing JOINs and
also only applies the _filter_query_attachment_filenames() filter
when a search query is being performed.

Props flixos90, joemcgill.
Fixes #22744.

File:
1 edited

Legend:

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

    r38625 r38733  
    11461146
    11471147    // Filter query clauses to include filenames.
    1148     add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     1148    if ( isset( $q['s'] ) ) {
     1149        add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     1150    }
    11491151
    11501152    return $q;
     
    11651167    remove_filter( 'posts_clauses', __FUNCTION__ );
    11661168
    1167     $clauses['join'] = " INNER JOIN {$wpdb->postmeta} ON ( {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id )";
     1169    // Add a LEFT JOIN of the postmeta table so we don't trample existing JOINs.
     1170    $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )";
     1171
    11681172    $clauses['groupby'] = "{$wpdb->posts}.ID";
    11691173
    11701174    $clauses['where'] = preg_replace(
    11711175        "/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/",
    1172         "$0 OR ( {$wpdb->postmeta}.meta_key = '_wp_attached_file' AND {$wpdb->postmeta}.meta_value $1 $2 )",
     1176        "$0 OR ( sq1.meta_value $1 $2 )",
    11731177        $clauses['where'] );
    11741178
Note: See TracChangeset for help on using the changeset viewer.