0

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?

Code:

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

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

<?php
$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( ', ' ) ); ?>
    </span>
    </div>
<?php endwhile; ?>
1
  • You could always conditionalise the query by checking if there's a user logged in, using is_user_logged_in() - function reference.
    – t31os
    Commented Mar 7, 2011 at 20:11

1 Answer 1

1

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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