Woocommerce Backend Search by Title and SKU

Question

Good morning,
i’m trying to extend backend product search by using this function without success.

This snippet works only in Title and i need to add also in Metakey SKU.

I have tried extend by using post_join and post_where as in this example
https://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
but i’m not expert on queries.

Please anyone can help me ?
Thanks in advance.

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
, , , , Marco F 2 years 2020-01-08T08:40:21-05:00 0 Answers 92 views 0

Leave an answer

Browse
Browse