WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/20/2016 01:44:07 AM (3 years ago)
Author:
joemcgill
Message:

Media: Make media library searchable by filename.

This applies a new private function, _filter_query_attachment_filenames(),
to the post_clauses filter hook during wp_ajax_query_attachments() and
wp_edit_attachments_query_vars() to include _wp_attached_file post meta
in search queries performed from the media library or in a WP_Media_List_Table.

Props wonderboymusic, DrewAPicture, joemcgill, swissspidy.
Fixes #22744.

File:
1 edited

Legend:

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

    r38572 r38625  
    11451145    }
    11461146
     1147    // Filter query clauses to include filenames.
     1148    add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     1149
    11471150    return $q;
     1151}
     1152
     1153/**
     1154 * Filter the SQL clauses of an attachment query to include filenames.
     1155 *
     1156 * @since 4.7.0
     1157 * @access private
     1158 *
     1159 * @param array $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY,
     1160 *                       DISTINCT, fields (SELECT), and LIMITS clauses.
     1161 * @return array The modified clauses.
     1162 */
     1163function _filter_query_attachment_filenames( $clauses ) {
     1164    global $wpdb;
     1165    remove_filter( 'posts_clauses', __FUNCTION__ );
     1166
     1167    $clauses['join'] = " INNER JOIN {$wpdb->postmeta} ON ( {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id )";
     1168    $clauses['groupby'] = "{$wpdb->posts}.ID";
     1169
     1170    $clauses['where'] = preg_replace(
     1171        "/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/",
     1172        "$0 OR ( {$wpdb->postmeta}.meta_key = '_wp_attached_file' AND {$wpdb->postmeta}.meta_value $1 $2 )",
     1173        $clauses['where'] );
     1174
     1175    return $clauses;
    11481176}
    11491177
Note: See TracChangeset for help on using the changeset viewer.