1

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.

0

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');
  • Will check and let you know in few mins... – Aditya Agarwal Aug 28 at 13:05
  • Hi, thanks it works. I am a newbie to php, can you please explain me why removing ' ' made it work, and please help out on when to use ' ' and when to not use them – Aditya Agarwal Aug 28 at 13:20
  • get_current_user_id() is a function, you can not call functions in single/double quotes. you can take reference for use quotes from scriptingok.com/tutorial/Single-quotes-vs-double-quotes-in-PHP – Chetan Vaghela Aug 28 at 13:30
  • Thank You, that's all that i need. Thanks a lot – Aditya Agarwal Aug 28 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.