Search the file name in the library

Inside the admin panel in Media → Library, how to search for media files using the file name in the "Search" field?

I know that the file name is placed in the "GUID" column in the database, but I do not know where I can find the piece of code responsible for searching the media, for example. The MySQL select.

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

+5
source share
2 answers

I found a solution in WordPress # 45153 developers , but here everything is related to 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;
}

, , :

$_GET['s'] $a_wp_query->query_vars['s'], , POST ajax, , " ".

+11

. ajax, .

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;
}
0

All Articles