php - Get Current User Id Inside a Loop Returns 0 For a Shortcode

I am having function, which has a loop for all the posts of given author. I use get_current_user_id() but that does not

I am having function, which has a loop for all the posts of given author. I use get_current_user_id() but that does not seem to work while inside a loop, or may be the shortcode troubles it. My function runs with the help of shortcode.

The current user always returns 0, and as a result it shows the meta for all the posts that ever existed on my site.

function get_meta_value_by_meta_key(){

$author_id = 'get_current_user_id()';
// do stuff to get user I
$author_posts = get_posts( array('author' => $author_id, 'numberposts' => -1 )); 
 // needed to collect the total sum of views
$counter = 0; 
// needed to collect the total sum of views echo 
'<h3>Post views by Author:</h3><ul>'; 
// do stuff to get author name
 foreach ( $author_posts as $post ) { 
$views = absint( get_post_meta( $post->ID, 'Creation_Views', true ) ); 
$counter += $views; 
echo "<li>{$post->post_title} ({$views})</li>"; 
} 
echo "</ul><hr /><p>Total Number of views: <strong>{$counter}</strong></p>";
   }
 add_shortcode('Stats', get_meta_value_by_meta_key);

How can i modify it to put the current user id, as the value of $author_id

I also tried to manually try to verify if everything else works, so i simply put my user id into $author_id and it just worked perfectly.

FYI: I took this code snippet from another answer on WPSE, the only thing was that, the answer didn't tell how to get current author id, and that's where the problem arises. I hope that this information helps.

I am having function, which has a loop for all the posts of given author. I use get_current_user_id() but that does not seem to work while inside a loop, or may be the shortcode troubles it. My function runs with the help of shortcode.

The current user always returns 0, and as a result it shows the meta for all the posts that ever existed on my site.

function get_meta_value_by_meta_key(){

$author_id = 'get_current_user_id()';
// do stuff to get user I
$author_posts = get_posts( array('author' => $author_id, 'numberposts' => -1 )); 
 // needed to collect the total sum of views
$counter = 0; 
// needed to collect the total sum of views echo 
'<h3>Post views by Author:</h3><ul>'; 
// do stuff to get author name
 foreach ( $author_posts as $post ) { 
$views = absint( get_post_meta( $post->ID, 'Creation_Views', true ) ); 
$counter += $views; 
echo "<li>{$post->post_title} ({$views})</li>"; 
} 
echo "</ul><hr /><p>Total Number of views: <strong>{$counter}</strong></p>";
   }
 add_shortcode('Stats', get_meta_value_by_meta_key);

How can i modify it to put the current user id, as the value of $author_id

I also tried to manually try to verify if everything else works, so i simply put my user id into $author_id and it just worked perfectly.

FYI: I took this code snippet from another answer on WPSE, the only thing was that, the answer didn't tell how to get current author id, and that's where the problem arises. I hope that this information helps.

Share Improve this question asked Aug 28, 2019 at 12:48 Aditya AgarwalAditya Agarwal 2764 silver badges22 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You have added get_current_user_id(); in single quote. please remove quote from 'get_current_user_id()';.

function get_meta_value_by_meta_key(){

   $author_id = get_current_user_id();
   // do stuff to get user I
   $author_posts = get_posts( array('author' => $author_id, 'numberposts' => -1 )); 
    // needed to collect the total sum of views
   $counter = 0; 
   // needed to collect the total sum of views 
   echo '<h3>Post views by Author:</h3><ul>'; 
    // do stuff to get author name
    foreach ( $author_posts as $post ) { 
       $views = absint( get_post_meta( $post->ID, 'Creation_Views', true ) ); 
       $counter += $views; 
       echo "<li>{$post->post_title} ({$views})</li>"; 
    } 
    echo "</ul><hr /><p>Total Number of views: <strong>{$counter}</strong></p>";
 }
 add_shortcode('Stats', 'get_meta_value_by_meta_key');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745205231a4616562.html

相关推荐

  • php - Get Current User Id Inside a Loop Returns 0 For a Shortcode

    I am having function, which has a loop for all the posts of given author. I use get_current_user_id() but that does not

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信