3

There are a lot of answers on how to count users posts by using:

<?php
    if ( is_user_logged_in() ) {
        global $wpdb;
    $user = wp_get_current_user();
    $where = get_posts_by_author_sql( 'page', true, $user->ID );
    $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" ); ?>

//option 1
<h2>Hey <?php echo $current_user->display_name ?>, check your page here: {page title with link to page} </h2>

<?php } else { ?>
//option 2
<h2>Welcome <?php echo $current_user->display_name ?>, etc.. text with tags etc.</h2>

<?php } } else { ?>
//option 3
<h2>text with tags etc.</h2>

<?php } ?>

But since users can also create pages, how does one get the count of current user's pages in a similar situation as the code above?

3

Look what count_user_posts() does inside and change the post type parameter:

global $wpdb;
// 'page' is the important part here
$where = get_posts_by_author_sql('page', true, $userid);
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );

Then you change your snippet to:

global $wpdb;
$user = wp_get_current_user();
$where = get_posts_by_author_sql( 'page', true, $user->ID );
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );

if ( $count >= 1 ) {

    // echo your text snippet

} else {

    // echo your form

}
  • Hi Tosho, can you please also explain how to add that code in the above example? – tobe Apr 19 '13 at 14:58
  • @tobe See my update. Caveat: I haven’t tested it. – fuxia Apr 19 '13 at 15:04
  • Great! it works like a charme! – tobe Apr 19 '13 at 17:47
  • Hi Toscho, what if I would like to display the page title of that user with link to that page? – tobe Apr 24 '13 at 7:55
  • @tobe See my update, just an idea, not tested. – fuxia Apr 24 '13 at 8:07

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.