I have a custom meta on my custom post type:
$arr = array('foo' => 'yes',
'featured_enabled' => 123,
'something' => 'abc',
);
update_post_meta($post_id, 'fvp_featured_meta', $arr );
My question is how can I get all products from specific category ID in woocommerce that have this meta (fvp_featured_meta)? (not all posts have this meta)
You can try to use wc_get_products()
function to get products that have a specific meta data belonging to a specific product category as follows:
$category_term_slugs = array('clothing; //
// Get an array of WC_Product Objects
$products = wc_get_products( array(
'limit' => -1,
'status' => 'publish',
'meta_key' => 'fvp_featured_meta',
'meta_compare' => 'EXISTS',
'category' => $category_term_slugs,
) );
foreach( $products as $product ) {
echo $product->get_name() . '<br>';
}
It should works.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments