修改主循环以显示日期用户注册后的帖子

时间:2016-03-21 23:17:36

标签: php wordpress loops

我已经玩了几天了,但根本没有成功。

我想修改index.php,以便主循环只显示在网站上注册的登录用户之后发布的帖子(内容已经为未登录的用户隐藏)

我可以获得注册日期,我可以修改循环,以便在特定日期之后显示帖子,但不能同时进行。

有人可以帮我解决这个问题吗?这是我到目前为止的代码 -

    <?php $regdate = date("Y-m-d", strtotime(get_userdata(get_current_user_id( ))->user_registered)); ?>

        <?php
        function filter_where($where = '') {
        $where .= " AND post_date >= '2016-02-18'";
        return $where;
        }
        add_filter('posts_where', 'filter_where');
        query_posts($query_string);
        ?>

1 个答案:

答案 0 :(得分:1)

这看起来像WP Query which already has established parameters to query via date.

的候选者

如果您提供的话,您的代码将如下所示:

<?php if ( have_posts() ): ?>
<?php
# Get the current user's info
$user_info = get_userdata(get_current_user_id());
# Use date_parse to cast your date to an array 
$regdate = date_parse($user_info->user_registered);
# Set your arguments for WP Query        
$args = array(
    'date_query' => array(
        array(
            'after'    => array(
                # Setting date to array above allows to call specific values within that date    
                'year'  => $regdate['year'],
                'month' => $regdate['month'],
                'day'   => $regdate['day'],
            ),
            # Include posts from the day the user registered  
            'inclusive' => true,
        ),
    ),
    # Display all posts on a single page.
    'posts_per_page' => -1,
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<article class="post post-<?php the_ID(); ?>" itemscope="itemscope" itemtype="http://schema.org/BlogPosting">
    <h2 class="blog__custom-content-title"><?php the_title();?></h2>
    <?php the_content();?>
</article>
<?php endwhile; ?>
<?php else: ?>
    <h2>No posts to display</h2>
<?php endif; ?>