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
1 Answer
Reset to default 2I 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
评论列表(0条)