<?php
$args22 = array(
'post_type' => array ('menus', 'recipes' ),
'author' => get_queried_object_id(),
'meta_query' => array(
'meta_key' => 'simplefavorites_count',
'value' => '0',
'compare' => '>=',
),
);
$my_query = new WP_Query( $args22 );
echo 'Found posts: ' . $my_query->found_posts . '<br />';
// declare variable and set total votes to 0
$total_fav_count = 0;
if ( $my_query->have_posts() ) {
while ( $my_query->have_posts() ) {}
$my_query->the_post();
// add custom field value to total votes
$total_fav_count += get_post_meta( get_the_ID(), 'simplefavorites_count', true );
}
}
//
echo 'Total user favorite votes: ' . $total_fav_count . '<br />';
wp_reset_postdata();
It’s also a good idea to check if simplefavorites_count
meta key exists:
<?php
$args22 = array(
'post_type' => array ('menus', 'recipes' ),
'author' => get_queried_object_id(),
'meta_query' => array(
'relation' => 'AND',
array(
'meta_key' => 'simplefavorites_count',
'value' => '0',
'compare' => '>=',
),
array(
'meta_key' => 'simplefavorites_count',
'compare' => 'EXISTS',
),
),
);