wp query - WP_Query with Metavalue

I have developed this function but returns 0 results. I think there is a problem with the way I am passing the meta_valu

I have developed this function but returns 0 results. I think there is a problem with the way I am passing the meta_value arguments. Any help?

add_shortcode( 'sc_count_brands', 'sc_count_brands_code' );
function sc_count_brands_code($atts) {
   $values = shortcode_atts( array('category'  => 'Horology',), $atts );
   $query = new WP_Query( array( 'post_type' => 'brands',  'post_status' => 'publish', 'meta_key' => 'br_category', 'meta_value' => esc_attr($values['category']) ) );
   $countn = $query->found_posts;
   $buffer = '<span class="magby">Featuring </span><span class="axiac">' . $countn . '</span><span class="magby"> brands</span>';
   wp_reset_postdata();
   return $buffer;
}

I have developed this function but returns 0 results. I think there is a problem with the way I am passing the meta_value arguments. Any help?

add_shortcode( 'sc_count_brands', 'sc_count_brands_code' );
function sc_count_brands_code($atts) {
   $values = shortcode_atts( array('category'  => 'Horology',), $atts );
   $query = new WP_Query( array( 'post_type' => 'brands',  'post_status' => 'publish', 'meta_key' => 'br_category', 'meta_value' => esc_attr($values['category']) ) );
   $countn = $query->found_posts;
   $buffer = '<span class="magby">Featuring </span><span class="axiac">' . $countn . '</span><span class="magby"> brands</span>';
   wp_reset_postdata();
   return $buffer;
}
Share Improve this question asked Oct 22, 2019 at 13:46 JoaMikaJoaMika 6986 gold badges27 silver badges58 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

I would recommand to add meta_query in array param, try this

add_shortcode( 'sc_count_brands', 'sc_count_brands_code' );
function sc_count_brands_code($atts) {
  $values = shortcode_atts( array('category'  => 'Horology',), $atts );
  $args = array(
    'post_type' => 'brands',
    'post_status' => 'publish',
    'meta_query' => array(
        array(
          'key'     => 'br_category',
          'value'   => esc_attr($values['category']),
          'compare' => '='
        ),
    ),
  );
  $query = new WP_Query( $args );
  $countn = $query->found_posts;
  $buffer = '<span class="magby">Featuring </span><span class="axiac">' . $countn . '</span><span class="magby"> brands</span>';
  wp_reset_postdata();
  return $buffer;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745061376a4608987.html

相关推荐

  • wp query - WP_Query with Metavalue

    I have developed this function but returns 0 results. I think there is a problem with the way I am passing the meta_valu

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信