Hi,
for gooloo.de +PLUS, our new ad-free solution, we wanted to enable that users of specific levels [in this code snippet levels 2 and 3, please change accordingly!] the ability to access all future, unpublished posts, that would be published in 7 days or max. That way, users of specific levels, can access posts 7 days earlier. For this, add the following code snippet to your functions.php, or via a Code Snippets Plugin.
What you have to do:
- Change the Level IDs accordingly to the levels you want to give access to
- Change the amount of days to your needs [this snippet uses 7]
/**
* Show future posts up to 7 days ahead for PMPro Levels 2 & 3 members
* Exclude pages and other post types
*/
function pmpro_show_future_posts_for_members($query) {
if (is_admin()) return;
// Get current post type being queried
$post_type = $query->get('post_type');
// Handle default post type (post)
if (empty($post_type)) {
if ($query->is_home() || $query->is_archive() || $query->is_search()) {
$post_type = 'post';
} else {
return;
}
}
// Only affect normal posts
if ($post_type !== 'post') return;
// Check membership access
$has_access = false;
if (is_user_logged_in()) {
$has_access = pmpro_hasMembershipLevel(array(2, 3));
}
if ($has_access) {
// Calculate 7 days from now
$current_time = current_time('timestamp');
$seven_days_later = date('Y-m-d H:i:s', $current_time + (7 * DAY_IN_SECONDS));
// Modify query for members
$query->set('post_status', array('publish', 'future'));
$query->set('date_query', array(
array(
'before' => $seven_days_later,
'inclusive' => true,
)
));
} else {
// Non-members see only published posts
$query->set('post_status', 'publish');
}
}
add_action('pre_get_posts', 'pmpro_show_future_posts_for_members');
/**
* Handle single post access for future posts
*/
function pmpro_check_single_future_post() {
if (!is_singular('post')) return;
global $post;
// Only check future posts
if ($post->post_status !== 'future') return;
// Verify membership and post date
$has_access = false;
if (is_user_logged_in()) {
$has_access = pmpro_hasMembershipLevel(array(2, 3));
}
$post_time = strtotime($post->post_date);
$cutoff_time = current_time('timestamp') + (7 * DAY_IN_SECONDS);
if (!$has_access || $post_time > $cutoff_time) {
global $wp_query;
$wp_query->set_404();
status_header(404);
nocache_headers();
}
}
add_action('wp', 'pmpro_check_single_future_post');
Leave a Reply