显示自上次用户登录时间/日期以来新添加的帖子

时间:2013-12-01 作者:Roee Yossef

我正在寻找一个解决方案来创建一个自定义循环,该循环将只显示自上次用户登录网站以来最后添加/修改的帖子。

应该没那么复杂吧?

1 个回复
最合适的回答,由SO网友:Chittaranjan 整理而成

在特定时间后获得帖子将分两步完成。

您需要存储用户的上次登录时间modified 在上述登录时间之后以下函数将存储用户的上次登录时间。

// Associating a function to login hook
add_action ( \'wp_login\', \'set_last_login\' );

function set_last_login ( $login ) {
    $user = get_userdatabylogin ( $login );

    // Setting the last login of the user
    update_usermeta ( $user->ID, \'last_login\', date ( \'Y-m-d H:i:s\' ) );
}
然后,您需要收集登录用户的上次登录时间,并按如下所示修改查询。

<?php
// Get current user object
$current_user = wp_get_current_user();

// Get the last login time of the user
$last_login_time = get_user_meta ( $current_user->ID, \'last_login\', true );

// WP_Query with post modified time
$the_query = new WP_Query(
                array(
                    \'date_query\' =>
                        array(
                            \'column\' => \'post_modified\',
                            \'after\' => $last_login_time,
                        )
                )
            );
?>
<?php if ( $the_query->have_posts() ) : ?>

    <?php // Start the Loop ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <?php // Show the output ?>
    <?php endwhile; ?>

<?php endif; ?>
<?php
// Restore original Post Data
wp_reset_postdata();
?>

结束

相关推荐

Add a button to users.php

我正在开发一个插件,为每个用户向用户添加元数据。通过添加列显示的php表。我已经这样做了,但我还想添加一个按钮,当按下时会删除用户的元数据。我不知道如何添加它。我希望它位于“更改”按钮的右侧。我以为这样做是为了重新加载页面,但当它这样做时,我会让它删除每个用户的元数据。我从哪里开始将该按钮添加到页面?这是最好的方式吗?谢谢