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 badges1 Answer
Reset to default 1You 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');