0

How to display the number of posts written by the logged in user.

Here is what i got from the codex so far

<?php echo 'Number of posts published by user: ' . count_user_posts( 5 ); ?>
  • Have you any code you've tried? – Aravona Nov 2 '15 at 10:55
  • This is from the codex. Im not that good at PHP, but it looks like its close to a solution: <?php echo 'Number of posts published by user: ' . count_user_posts( 5 ); ?> – Demilio Nov 2 '15 at 11:19
  • What is the problem here? Does your code work? If not what is not working? – Sisir Nov 2 '15 at 11:30
  • 1
    @Demilio that's going to display the number of posts for the user with an id of 5, so if you want to display the ones for the logged in user, you'll need to use <?php $user_ID = get_current_user_id(); ?> and then use count_user_posts( $user_ID ); or something like that - that's just off the top of my head. – Aravona Nov 2 '15 at 11:42
1

Here a code example with explanation in the comments:

// First a check whether the use is logged in or not:
if ( is_user_logged_in() ) {
   // The user is logged, retrieve the user id
    $user_ID = get_current_user_id(); 
   // Now we have got the user's id, we can pass it to the function 'count_user_posts':
    echo 'Number of posts published by user: ' . count_user_posts( $user_ID );
} else {
    //  The user is not logged in
}
  • 1
    Could you explain your code rather than just post raw code. Many people here seeking help have very limited understanding of PHP. – s_ha_dum Nov 2 '15 at 14:17
  • Good point, thanks for the advice, better like this? – Tobias Beuving Nov 2 '15 at 15:17

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.