admin管理员组文章数量:1416625
I found the below code to count post views in WordPress. I needed 2 things from this, which I am not able to do yet.
Author stats: How to show the current logged in user, the total views of all the published posts via shortcode. How can I handle this? I found an answer on WPSE for showing the total views but it doesn't add shortcode and gets views of all posts, I want views of all the published posts
For representing the data, how to show the views of each and every author in a html table, not just current author, all the authors, just the author name and total post views. This can be useful for multi author sites and can be used to track performance of all the authors.
// function to display number of posts.
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 View";
}
return $count.' Views';
}
// function to count views.
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);
}
}
I tried to do a array of all the post ids of current user but I failed. I also tried to add a Author id, portion in this post meta to make easy. But none seemed to work.
Here's the current code implementation
function getPostViews($postID){
$count_key = 'Creation_Views';
$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 View";
}
return $count.' Views';
}
function setPostViews($postID) {
$count_key = 'Creation_Views';
$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);
}
}
function get_meta_value_by_meta_key($key, $values, $sum){
global $wpdb;
$metaValues = array();
$key= 'Creation_views';
$query = "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '$key'";
$results = $wpdb->get_results($query);
var_dump($results);
if(!empty($results) && !is_null($results)){
foreach($results as $result){
foreach($result as $value){
$metaValues[] = $value;
}
}
}
//get_results returns null on failure
if(is_null($results) && WP_DEBUG == true){
$wpdb->show_errors();
$wpdb->print_error();
}
return $metaValues;
$values = get_meta_value_by_meta_key('Creation_views');
$sum = array_sum(array_map('intval', $values));
}
add_shortcode('Stats','get_meta_value_by_meta_key');
I found the below code to count post views in WordPress. I needed 2 things from this, which I am not able to do yet.
Author stats: How to show the current logged in user, the total views of all the published posts via shortcode. How can I handle this? I found an answer on WPSE for showing the total views but it doesn't add shortcode and gets views of all posts, I want views of all the published posts
For representing the data, how to show the views of each and every author in a html table, not just current author, all the authors, just the author name and total post views. This can be useful for multi author sites and can be used to track performance of all the authors.
// function to display number of posts.
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 View";
}
return $count.' Views';
}
// function to count views.
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);
}
}
I tried to do a array of all the post ids of current user but I failed. I also tried to add a Author id, portion in this post meta to make easy. But none seemed to work.
Here's the current code implementation
function getPostViews($postID){
$count_key = 'Creation_Views';
$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 View";
}
return $count.' Views';
}
function setPostViews($postID) {
$count_key = 'Creation_Views';
$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);
}
}
function get_meta_value_by_meta_key($key, $values, $sum){
global $wpdb;
$metaValues = array();
$key= 'Creation_views';
$query = "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '$key'";
$results = $wpdb->get_results($query);
var_dump($results);
if(!empty($results) && !is_null($results)){
foreach($results as $result){
foreach($result as $value){
$metaValues[] = $value;
}
}
}
//get_results returns null on failure
if(is_null($results) && WP_DEBUG == true){
$wpdb->show_errors();
$wpdb->print_error();
}
return $metaValues;
$values = get_meta_value_by_meta_key('Creation_views');
$sum = array_sum(array_map('intval', $values));
}
add_shortcode('Stats','get_meta_value_by_meta_key');
Share
Improve this question
edited Aug 11, 2019 at 3:35
Aditya Agarwal
asked Aug 9, 2019 at 11:43
Aditya AgarwalAditya Agarwal
2764 silver badges22 bronze badges
1 Answer
Reset to default 0You have to create custom query, to get data from {$wpdb->prefix}postmeta
depending on the meta_key
and store the meta_value
s inside an array, then sum them.
So we can build a function like this:
/**
* Query meta values by meta key.
*
* @return int Returns sum of meta values
*/
function get_meta_value_by_meta_key(){
global $wpdb;
$metaValues = array();
$query = "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'Creation_Views'";
$results = $wpdb->get_results($query);
if(!empty($results) && !is_null($results)){
foreach($results as $result){
foreach($result as $value){
$metaValues[] = $value;
}
}
}
//get_results returns null on failure
if(is_null($results) && WP_DEBUG == true){
$wpdb->show_errors();
$wpdb->print_error();
}
$sum = array_sum(array_map('intval', $metaValues));
return $sum;
}
add_shortcode('Stats', get_meta_value_by_meta_key());
本文标签: phpSum All the Post Meta of Published posts of Current Logged in user
版权声明:本文标题:php - Sum All the Post Meta of Published posts of Current Logged in user 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745252231a2649890.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论