Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search by file name in the Media Library

Tags:

wordpress

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.

like image 519
Arek - Krakiewicz.pl Avatar asked Feb 18 '13 15:02

Arek - Krakiewicz.pl


1 Answers

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

  • How to filter post listing using a custom field?

  • Limiting Admin Backend Search to Title

  • Better search for Wordpress admin backend

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.

like image 198
brasofilo Avatar answered Nov 15 '22 07:11

brasofilo