WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using the following code to get ONLY posts with the post_type question and the current user who is logged in.

But if no use is logged in all the posts are displayed anyways.

Any suggestions?


<?php global $current_user; ?>
<?php get_currentuserinfo(); ?>

<?php echo 'Username: ' . $current_user->user_login . "\n"; ?>

$args = array(
    'post_type' => 'question',
    'post_author'  => $current_user->ID)

<?php $custom_posts = new WP_Query(); ?>
<?php $custom_posts->query($args); ?>
<?php while ($custom_posts->have_posts()) : $custom_posts->the_post(); ?>
    <div class="block-2 border-top">
        <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
        <?php the_excerpt(); ?>
        <p><?php comments_number('0 Replies','1 Reply','% Replies'); ?>.</p>
        <p><?php display_votes(get_the_ID()); ?></p>
        <p><?php the_views(); ?></p>
    <span class="cat-links">
        <?php printf( __( '<span class="%1$s">Posted in</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?>
<?php endwhile; ?>
share|improve this question
You could always conditionalise the query by checking if there's a user logged in, using is_user_logged_in() - function reference. – t31os Mar 7 '11 at 20:11
up vote 1 down vote accepted

The variable is not valorize: $current_user->ID is 0, so the query extracts all post, try to give it a non existent integer value

Try to substitute $current_user->ID with

($current_user->ID==0) : -1 ? $current_user->ID

It assign -1 to the author, if there is no user. So the query must return no results!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.