我正在使用以下代码来获取post_type问题和登录的当前用户的帖子。

但是,如果未记录所有用途,则所有帖子都将显示。

有什么建议么?

代码:

<?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; ?>
有帮助吗?

解决方案

该变量没有价值: $current_user->ID0, ,因此查询提取所有帖子,尝试给它一个不存在的整数值

尝试替代 $current_user->ID

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

如果没有用户,它将-1分配给作者。因此,查询必须返回不返回结果!

许可以下: CC-BY-SA归因
scroll top