Get all Products by Custom Field ACF Id and Value in Woocommerce ? mình muốn lấy danh sách tất cả các sản phẩm theo Field ACF nhập vào. VD: mình có cái nhóm sản phẩm: group-product = 12, mình nhập một số sản phẩm có group-product = 12 và lấy nó ra trong code ở funtion.php để xử lý thì làm ntn. Mình làm giống trên mạng mà nó không ra Mã: $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1, 'meta_key' => 'group-product', //Your Custom field name 'meta_value' => '12 ', //Custom field value 'meta_compare' => '=' ); $the_query = new WP_Query( $args ); $count = $the_query->post_count; if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); $the_query->the_post(); echo '<li>' . get_the_title() . '</li>'; endwhile; endif;
Code trên mình thấy đúng, sao không ra kết quả được, hoặc bạn sử dụng code sau nhé, mình đã sử dụng qua và lấy được giá trị bình thường. PHP: $args = array( 'post_type' => 'product', 'meta_key' => 'group-product', //feild name 'meta_value' => array('nhaxaydung'), //'meta_value' => array('yes'), feild value 'meta_compare' => 'IN' //'meta_compare' => 'NOT IN');$productslist = wc_get_products($args);//lấy danh sách sản phẩmforeach ($productslist as $pro) { //xử lý sản phẩm lấy được ở đây // $pro->get_id(); //$pro->get_permalink() //get_the_post_thumbnail($pro->get_id(), 'thumnail', array( 'class' =>'thumnail') ) //$pro->get_title() //$pro->get_price_html()}