WP_Query with Metavalue

时间:2019-10-22 作者:JoaMika

我开发了此函数,但返回0个结果。我认为传递meta\\u值参数的方式有问题。有什么帮助吗?

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;
}

1 个回复
最合适的回答,由SO网友:Fab 整理而成

我建议在array param中添加meta\\u查询,请尝试以下操作

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;
}

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post