<?php
$QUERY = new Wp_query(array(
\'post_type\' => \'post_type_here\',
\'category_name\' => \'category_name_here\',
// you can use cat id ...
));
$counter = 0;
while( $QUERY->have_posts() ) : $QUERY->the_post();
$views = absint( getPostViews( $post->ID ) );
$counter += $views;
endwhile; wp_reset_postdata();
echo $counter;
?>
getPostViews()和setPostViews:
function getPostViews( $postID ){
$count_key = \'post_views_count\';
$count = get_post_meta($postID, $count_key, true);
if($count==\'\'){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, \'0\');
return "0";
}
return $count;
}
function setPostViews( $postID ) {
$count_key = \'post_views_count\';
$count = get_post_meta($postID, $count_key, true);
if($count==\'\'){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, \'0\');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}