0

I want to announce the logged-in user in front end (theme, not the backend), how many posts he has with htis statuses:

  1. awaiting moderation
  2. approved (published)
  3. not approved(rejected)

since last announcement. Are there any plugins for this? Are there any good keywords to search for such a thing?
thanks.

1 Answer 1

1

You can create an easy to use function to do that for you.

Like so:

<?php
function sg_user_posts_by_type($userid, $post_status) {
    $args = array(
        'numberposts'   => -1,
        'post_status'   => array($post_status),
        'author'        => $userid
    );
    $count_posts = count(get_posts($args)); 
    return $count_posts;
}


if (is_user_logged_in()) {

    // GET CURRENT USER DATA
    $user_id            = get_current_user_id();
    $user_data          = get_userdata($user_id);

    $publish_count      = sg_user_posts_by_type($user_id, 'publish');
    $pending_count      = sg_user_posts_by_type($user_id, 'pending');
    $draft_count        = sg_user_posts_by_type($user_id, 'draft');


    echo '
        <h3>Hey '.$user_data->last_name.', You got:</h3>
        <ul>
            <li>'.$publish_count.' published Posts</li>
            <li>'.$pending_count.' pending Posts</li>
            <li>'.$draft_count.' drafts</li>
        </ul>
    ';
}
?>

you could make it less resource costly if you need to but this should work fine.
Hope this helps ;)

2
  • Thanks for your reponse. what do you mean by you could make it last resource costly if you need to. my english sucks.
    – biji buji
    Jun 14, 2014 at 7:26
  • 1
    i mean you could use wp_query instead of get_posts and ask for IDS only in the query to make it return exactly what you need and not the entire post objects.
    – Sagive
    Jun 14, 2014 at 16:36

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.