Php wordpress通过元数据和元数据获取产品
我试图通过元值和元键来显示产品。我使用下面的代码来验证meta_键和meta_值。现在,如果没有值为“yes\u feat”的meta\u键,则不会打印产品。太棒了 问题是,如果只有一个产品的meta_键的值为“yes_feat”,那么所有其他产品也会打印出来。我如何使其仅显示具有meta_值“yes_feat”的产品Php wordpress通过元数据和元数据获取产品,php,wordpress,woocommerce,wordpress-theming,hook-woocommerce,Php,Wordpress,Woocommerce,Wordpress Theming,Hook Woocommerce,我试图通过元值和元键来显示产品。我使用下面的代码来验证meta_键和meta_值。现在,如果没有值为“yes\u feat”的meta\u键,则不会打印产品。太棒了 问题是,如果只有一个产品的meta_键的值为“yes_feat”,那么所有其他产品也会打印出来。我如何使其仅显示具有meta_值“yes_feat”的产品 非常感谢在WP\u查询参数中添加元查询 <?php $params = array( 'post_type' => 'product', 'meta_
非常感谢在WP\u查询参数中添加元查询
<?php
$params = array(
'post_type' => 'product',
'meta_query' => array(
array('key' => '_featured', //meta key name here
'value' => 'yes_feat',
'compare' => '=',
)
),
'posts_per_page' => 5
);
$wc_query = new WP_Query($params);
global $post, $product;
if( $wc_query->have_posts() ) {
while( $wc_query->have_posts() ) {
$wc_query->the_post();
$wc_query->post_name;
echo "print products that have meta_value = yes_feat";
$yes_feat = get_post_meta($wc_query->ID, '_featured',true );
} // end while
} // end if
else
{
echo "nothing found";
}
wp_reset_postdata();
?>
您的代码似乎正常。如果没有额外的因素,它不可能给出所有的产品。使用此代码,在have_posts循环中打印您的最终查询,并将结果粘贴到此处$i=0;if($wc_query->have_posts()){$i++;if($i==1)var_dump($wc_query->query);}
<?php
$params = array(
'post_type' => 'product',
'meta_query' => array(
array('key' => '_featured', //meta key name here
'value' => 'yes_feat',
'compare' => '=',
)
),
'posts_per_page' => 5
);
$wc_query = new WP_Query($params);
global $post, $product;
if( $wc_query->have_posts() ) {
while( $wc_query->have_posts() ) {
$wc_query->the_post();
$wc_query->post_name;
echo "print products that have meta_value = yes_feat";
$yes_feat = get_post_meta($wc_query->ID, '_featured',true );
} // end while
} // end if
else
{
echo "nothing found";
}
wp_reset_postdata();
?>