Skip to content

Instantly share code, notes, and snippets.

@mircian
Last active October 29, 2021 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mircian/45213bd9a3483de0ffd277a4b3ee47f3 to your computer and use it in GitHub Desktop.
Save mircian/45213bd9a3483de0ffd277a4b3ee47f3 to your computer and use it in GitHub Desktop.
/**
* Alter the query vars to include products which have the meta we are searching for.
*
* @param array $query_vars The current query vars.
*
* @return array
*/
function m_request_query( $query_vars ) {
global $typenow;
global $wpdb;
global $pagenow;
if ( 'product' === $typenow && isset( $_GET['s'] ) && 'edit.php' === $pagenow ) {
$search_term = esc_sql( sanitize_text_field( $_GET['s'] ) );
$meta_key = '_your_meta_key';
$post_types = array( 'product', 'product_variation' );
$search_results = $wpdb->get_results(
$wpdb->prepare(
"SELECT DISTINCT posts.ID as product_id, posts.post_parent as parent_id FROM {$wpdb->posts} posts LEFT JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id WHERE postmeta.meta_key = '{$meta_key}' AND postmeta.meta_value LIKE %s AND posts.post_type IN ('" . implode( "','", $post_types ) . "') ORDER BY posts.post_parent ASC, posts.post_title ASC",
'%' . $wpdb->esc_like( $search_term ) . '%'
)
);
$product_ids = wp_parse_id_list( array_merge( wp_list_pluck( $search_results, 'product_id' ), wp_list_pluck( $search_results, 'parent_id' ) ) );
$query_vars['post__in'] = array_merge( $product_ids, $query_vars['post__in'] );
}
return $query_vars;
}
add_filter( 'request', 'm_request_query', 20 );
/**
* Alter the query vars to include products which have the meta we are searching for.
*
* @param array $query_vars The current query vars.
*
* @return array
*/
function m_request_query( $query_vars ) {
global $typenow;
global $wpdb;
global $pagenow;
if ( 'product' === $typenow && isset( $_GET['s'] ) && 'edit.php' === $pagenow ) {
$search_term = esc_sql( sanitize_text_field( $_GET['s'] ) );
// Split the search term by comma.
$search_terms = explode( ',', $search_term );
// If there are more terms make sure we also search for the whole thing, maybe it's not a list of terms.
if ( count( $search_terms ) > 1 ) {
$search_terms[] = $search_term;
}
// Cleanup the array manually to avoid issues with quote escaping.
array_walk( $search_terms, 'trim' );
array_walk( $search_terms, 'esc_sql' );
$meta_key = '_sku';
$post_types = array( 'product', 'product_variation' );
$query = "SELECT DISTINCT posts.ID as product_id, posts.post_parent as parent_id FROM {$wpdb->posts} posts LEFT JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id WHERE postmeta.meta_key = '{$meta_key}' AND postmeta.meta_value IN ('" . implode( "','", $search_terms ) . "') AND posts.post_type IN ('" . implode( "','", $post_types ) . "') ORDER BY posts.post_parent ASC, posts.post_title ASC";
$search_results = $wpdb->get_results( $query );
$product_ids = wp_parse_id_list( array_merge( wp_list_pluck( $search_results, 'product_id' ), wp_list_pluck( $search_results, 'parent_id' ) ) );
$query_vars['post__in'] = array_merge( $product_ids, $query_vars['post__in'] );
}
return $query_vars;
}
add_filter( 'request', 'm_request_query', 20 );
@toddbenton
Copy link

I got this message today from the WordPress "Your Site is Experiencing a Technical Issue" notification system.

"An error of type E_ERROR was caused in line 332 of the file /public_html/wp-content/themes/pro-child/functions.php. Error message: Uncaught TypeError: array_merge(): Argument #2 must be of type array, null given in /public_html/wp-content/themes/pro-child/functions.php:332"

Line 332 in my functions.php corresponds with line 30 in the wp_admin_product_sku_multiple_terms.php code snippet. Is there an update to this code? I updated to PHP 8 recently and suspect that may be the reason why I am getting this error message.

@mircian
Copy link
Author

mircian commented Oct 29, 2021

Hi @toddbenton,

From the error message, it looks like you have to make sure that $query_vars['post__in'] is an array as PHP8 is stricter.

Adding something like this before line 30 should do the trick:

if ( ! isset( $query_vars['post__in'] ) || is_null( $query_vars['post__in'] ) ) {
 $query_vars['post__in'] = [];
}

@toddbenton
Copy link

Thanks Mircea. I was hoping you would be able to reply to this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment