Searching products by tittle name ( Search bar ) backend wordpress woocommerce when is active it messes up the search in Media library

Viewed 15

This code works perfect to search products by title on woocommerce .. ( dashbord backend ). However, it's affecting the search on media library, When the code is active it messes up the search in Media library Wordpress.... Is it possible to change the code to target only products instead of images, etc - media library. Thank you.

function __search_by_title_only( $where, &$wp_query )
{
    global $wpdb,$typenow,$pagenow;

    if ( 'product' === $typenow && isset( $_GET['s'] ) && 'edit.php' === $pagenow ) {
        $search_ids = array();
        $terms      = explode( ' ', $_GET['s'] );

        foreach ( $terms as $term ) {
                if ( is_numeric( $term ) ) {
                    $search_ids[] = $term;
                }

                if ( $search_term = $_GET['s'] ) {
                    $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $term ) ) . '%\'';
                }

        }
        return $where;  
    }
}
add_filter('posts_search', '__search_by_title_only', 500, 2);      



0 Answers
Related