您可以计算用户可以看到的帖子数量,如下所示:(int) ( ( time() - strtotime( get_userdata(get_current_user_id( ))->user_registered ) ) / ( 7 * 24 * 60 * 60 ) ) + 1;
因此,您为pre_get_posts
为了向用户显示他在您的网站上花费的尽可能多的帖子,请在几周内完成。
为了防止看到自定义输入链接的内容,您将过滤器添加到内容中,检查当前帖子是否在当前用户可用的帖子中。
当然,您可以为管理员显示所有内容
add_filter( \'pre_get_posts\', \'prefix_pre_get_posts\' );
function prefix_pre_get_posts( $query ) {
if( ! is_admin() && \'post\' == $query->post_type ) { // is_main_query() ?
$post_pre_page = null;
if( is_user_logged_in() && ! current_user_can( \'administrator\' ) ) {
$post_pre_page = (int) ( ( time() - strtotime( get_userdata(get_current_user_id( ))->user_registered ) ) / ( 7 * 24 * 60 * 60 ) ) + 1;
} elseif( ! is_user_logged_in() ) {
$post_pre_page = 0;
}
if( ! is_null( $post_pre_page ) ) {
$query->set( \'posts_per_page\', $post_pre_page );
// Show pastest posts first
$query->set( \'order\', \'DESC\' );
$query->set( \'orderby\', \'date\' );
}
}
}
add_filter( \'the_content\', \'prefix_filter_the_content\' );
function prefix_filter_the_content( $content ) {
if( current_user_can( \'administrator\' ) )
return $content;
$current_id = get_the_id();
$allowed_posts = get_posts( array( \'posts_per_page\' => -1 ) );
$flag = false;
if( ! empty( $allowed_posts ) )
foreach( $allowed_posts as $p )
if( $current_id == $p->ID ){
$flag = true;
break;
}
if( ! $flag ) {
return __( \'Your are not able to see these post\', \'your-textdomain\' );
}
return $content;
}
只需将这些代码添加到
functions.php
.
注:将7天更改为3天(例如),请在此处更改(7 * 24*60*60)“至”(3 * 24*60*60)\'