5

Inside the Admin Panel, in Media -> Library, how to search media files by file name in "search media" box?

I know the file name is placed in the "GUID" column in the database, but I don't know where I can find the piece of code responsible for media search, ex. MySQL select.

Now it only searches in the post_title column. I also tried to find $_REQUEST['s'], but with without result.

2 Answers 2

10

I found a solution in WordPress Developers #45153, but here are all related Q&A's:

add_filter( 'posts_search', 'guid_search_so_14940004', 10, 2 );

function guid_search_so_14940004( $search, $a_wp_query ) 
{
    global $wpdb, $pagenow;

    // Only Admin side && Only Media Library page
    if ( !is_admin() && 'upload.php' != $pagenow ) 
        return $search;

    // Original search string:
    // AND (((wp_posts.post_title LIKE '%search-string%') OR (wp_posts.post_content LIKE '%search-string%')))
    $search = str_replace(
        'AND ((', 
        'AND (((' . $wpdb->prefix . 'posts.guid LIKE \'%' . $_GET['s'] . '%\') OR ', 
        $search
    ); 

    return $search;
}

There was a suggestion that I need to verify, but seems good:

Change $_GET['s'] to $a_wp_query->query_vars['s'] so that this also works when the search function is called via an ajax POST, eg when using the "Create Gallery" dialogue.

3
  • Works great. Just put it in functions.php in your theme catalog. Thank You! Feb 26, 2013 at 13:50
  • 5
    @Arek-Krakiewicz.pl, it's better to create your own plugin ;)
    – brasofilo
    Feb 26, 2013 at 13:57
  • Neat solution so far. But be aware that the media search is extended by looking for the guid value which by deafult is the URL of post entries (respectively of the files). So, if let's say your domain is example.com and your uploaded media file's name is example-image.jpg you would get all files as result when searching for "example".
    – Arvid
    Apr 29, 2016 at 15:09
0

Quick update to this answer. The code above only works for the Media Library options page and doesn't apply to any ajax requests which occur when inserting an image into the content area.

add_filter( 'posts_search', 'guid_search_so_14940004', 10, 2 );

function guid_search_so_14940004( $search, $a_wp_query ) {
    global $wpdb, $pagenow;

    // Only Admin side && Only Media Library page
    if ( ! is_admin() ) {
        return $search;
    }

    if ( 'admin-ajax.php' !== $pagenow && 'upload.php' !== $pagenow ) {
        return $search;
    }

    if ( isset( $_REQUEST['action'] ) && 'query-attachments' === $_REQUEST['action'] && ! empty( $_REQUEST['query']['s'] ) ) {
        $search_string = $_REQUEST['query']['s'];
    }

    if ( isset( $_REQUEST['s'] ) || empty( $_REQUEST['s'] ) ) {
        $search_string = $_REQUEST['s'];
    }

    if ( ! isset( $search_string ) || empty( $search_string ) ) {
        return $search;
    }

    // Original search string:
    // AND (((wp_posts.post_title LIKE '%search-string%') OR (wp_posts.post_content LIKE '%search-string%')))
    $search = str_replace(
        'AND ((',
        'AND (((' . $wpdb->posts . '.guid LIKE \'%' . $wpdb->esc_like( $search_string ) . '%\') OR ',
        $search
    );

    return $search;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.