WordPress.org

Support

Support » Plugins and Hacks » [Resolved] How to retrieve the number of pending posts for an author?

[Resolved] How to retrieve the number of pending posts for an author?

  • I’ve got a site where I’d like to keep track of how many “pending” posts each user has. I think I can use a query with $wpdb->get_var to do this, but am having trouble with it.

    Here’s the code I’m using, based on what I found at another topic:

    global $wpdb;
    $where = "WHERE post_status = 'pending' AND post_author = " . $user_id;
    $post_count = $wpdb->get_var(
    "SELECT COUNT (*) FROM $wpdb->posts $where");
    $welcomemessage .= "<p>You currently have $post_count submissions pending.</p>";

    Problem is, $post_count doesn’t seem to be getting any data at all. The output is simply “You currently have submissions pending.”

    Am I doing something wrong here?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Any ideas why this isn’t working? Am I posting this in the wrong forum?

    <?php
    global $wpdb;
    $query = "SELECT COUNT(*) FROM wp30_posts WHERE post_status = 'pending' AND post_author = '$user_id'";
    $post_count = $wpdb->get_var($query);
    $welcomemessage .= "<p>You currently have $post_count submission(s) pending.</p>";
    echo $welcomemessage
    ?>

    Aha! It works now. THANK YOU!

    So I was using $wpdb->posts in my query instead of wp_posts. That explains a lot. Also looks like I needed to enclose the $user_id in single quotes.

    Correcting the typo in your example, here’s the working code:

    global $wpdb;
    $query = "SELECT COUNT(*) FROM wp_posts WHERE post_status = 'pending' AND post_author = '$user_id'";
    $post_count = $wpdb->get_var($query);
    $welcomemessage .= "<p>You currently have $post_count submission(s) pending.</p>";
    echo $welcomemessage
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] How to retrieve the number of pending posts for an author?’ is closed to new replies.
Skip to toolbar