使用 WP 查询循环访问 WooCommerce 产品的元值
Loop through the meta value of WooCommerce products using a WP Query
我在 WooCommerce API 工作,我想创建一个新端点来列出我的特定产品。我们已经用插件制作了以前的新字段,我想检查一下。如果设置中勾选了属性则显示,否则不显示
新的属性产品(高级设置中的复选框):
woocommerce_wp_checkbox(
array(
'id' => '_checkbox_badge_hot',
'desc' => __('set the checkbox', 'woocommerce'),
'label' => __('Hot', 'woocommerce'),
'desc_tip' => 'true',
'value' => get_post_meta( $post->ID, '_checkbox_badge_hot', true )
)
);
应该列出产品的代码(现在,如果我尝试向端点请求,它只会不断加载):
function get_hot_products(){
$args_for_hot_product = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$loop = new WP_Query( $args_for_hot_product );
$hotproducts = [];
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
while ( $loop->have_posts() ): $loop->the_post(){
$hot = $loop->get_meta('_checkbox_badge_hot');
if( $hot === "yes" ){
array_push($hotproducts, $hot);
}
}
//wp_reset_postdata();
return $hotproducts;
}
add_action( 'rest_api_init', function () {
register_rest_route( '/wc/v3', '/featuredproducts', array(
'methods' => 'GET',
'callback' => 'get_hot_products',
) );
} );
感谢大家的帮助!
要获取并显示具有特定自定义字段值的所有产品,使用 WP_Query
,您只需使用如下 eta 查询:
function display_hot_products(){
$loop = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array( array(
'key' => '_checkbox_badge_hot',
'value' => 'yes',
'compare' => '=',
)),
));
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
the_title(); // Display product title
endwhile;
wp_reset_postdata();
endif;
}
不要忘记,当使用 WP_Query
时,您会得到 WC_Post
个对象,而不是 WC_Product
个对象。
我在 WooCommerce API 工作,我想创建一个新端点来列出我的特定产品。我们已经用插件制作了以前的新字段,我想检查一下。如果设置中勾选了属性则显示,否则不显示
新的属性产品(高级设置中的复选框):
woocommerce_wp_checkbox(
array(
'id' => '_checkbox_badge_hot',
'desc' => __('set the checkbox', 'woocommerce'),
'label' => __('Hot', 'woocommerce'),
'desc_tip' => 'true',
'value' => get_post_meta( $post->ID, '_checkbox_badge_hot', true )
)
);
应该列出产品的代码(现在,如果我尝试向端点请求,它只会不断加载):
function get_hot_products(){
$args_for_hot_product = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$loop = new WP_Query( $args_for_hot_product );
$hotproducts = [];
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
while ( $loop->have_posts() ): $loop->the_post(){
$hot = $loop->get_meta('_checkbox_badge_hot');
if( $hot === "yes" ){
array_push($hotproducts, $hot);
}
}
//wp_reset_postdata();
return $hotproducts;
}
add_action( 'rest_api_init', function () {
register_rest_route( '/wc/v3', '/featuredproducts', array(
'methods' => 'GET',
'callback' => 'get_hot_products',
) );
} );
感谢大家的帮助!
要获取并显示具有特定自定义字段值的所有产品,使用 WP_Query
,您只需使用如下 eta 查询:
function display_hot_products(){
$loop = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array( array(
'key' => '_checkbox_badge_hot',
'value' => 'yes',
'compare' => '=',
)),
));
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
the_title(); // Display product title
endwhile;
wp_reset_postdata();
endif;
}
不要忘记,当使用 WP_Query
时,您会得到 WC_Post
个对象,而不是 WC_Product
个对象。