wordpress通过元数据和元值获取产品

我正在尝试通过meta值和meta键显示产品。我正在使用下面的代码来验证meta_key和meta_value。现在, 如果没有值为” yes_feat”的meta_keys, 则不会打印任何产品。太好了!

问题是, 如果只有1个产品的meta_keys值为” yes_feat”, 则其他所有产品也会同时打印。如何做到这一点, 只有显示meta_value为” yes_feat”的产品才能显示

        $params = array(

            'post_type' => 'product', 'meta_key' => '_featured', 'meta_value' => 'yes_feat', 'posts_per_page' => 5 

        );
        $wc_query = new WP_Query($params);
        global $post, $product;

        if ($wc_query->have_posts()) { 
            // i am only trying to print products with meta_value = yes_feat
            // but if the statement above is true it prints all products
             echo "print products that have meta_value = yes_feat";
        }   
        else 
        {
            echo "nothing found";
        }

非常感谢


#1


在WP_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();
?>

来源:

https://www.srcmini02.com/66135.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37139.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?