Search by file name in the Media Library

2019-04-29 15:43发布

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.

标签: wordpress
1条回答
Root(大扎)
2楼-- · 2019-04-29 16:18

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.

查看更多
登录 后发表回答