We often have to do queries for post IDs which we then use for a later query. However, I don’t think WordPress has a way to return just the result set we want which is a one dimensional array of post_ids. Instead it returns an array of objects (or arrays) with a post_id property. One way to get those post_ids into their own one dimensional array is to loop through your results and pick them out one by one…boring.
If you run into a similar problem try using array_columns to automagically pull a one dimensional array of post_ids out of a two dimensional array.
1 2 3 4 5 6 7 8 |
//Get our post_ids $product_ids = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM pkwp_postmeta WHERE (meta_key='_sku' OR meta_key='_product_sku') AND meta_value REGEXP %s", '.{2} '.$_GET['state'].'.*' ) ); // Pull our our post_ids into it's own array $product_ids = array_column($product_ids, 'post_id'); // Use this new array to limit a different post's results. $query->set('post__in', $product_ids); |