WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to retrieve the number of pending posts for an author? (4 posts)

  1. dtweney
    Member
    Posted 4 years ago #

    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?

  2. dtweney
    Member
    Posted 4 years ago #

    Any ideas why this isn't working? Am I posting this in the wrong forum?

  3. MichaelH
    Member
    Posted 4 years ago #

    <?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
    ?>
  4. dtweney
    Member
    Posted 4 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic