Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Created September 9, 2014 17:45
Show Gist options
  • Save strangerstudios/819d654498218f262abd to your computer and use it in GitHub Desktop.
Save strangerstudios/819d654498218f262abd to your computer and use it in GitHub Desktop.
With Paid Memberships Pro, remove public access to posts that were published over 24 hours ago.
/*
This code will create a content filter for all pages and posts to remove public access to posts that were published over 24 hours ago. Only posts or pages which require membership will be hidden.
The params passed are:
$hasaccess - (bool) what PMPro thinks about whether the user has access
$thepost - (post object) the post being checked, usually the current post
$theuser - (user object) the user being checked, usually the current user
$post_membership_levels - (array of levels) the levels this post requires (if any)
*/
add_filter("pmpro_has_membership_access_filter", "hide_old_posts_from_public", 10, 4);
function hide_old_posts_from_public($hasaccess, $thepost, $theuser, $post_membership_levels)
{
global $wpdb;
//if the post doesn't require membership, allow access
if(!$post_membership_levels)
return true;
//okay, this post requires membership. start by getting the user's startdate
$cutoff = strtotime("-24 Hours", current_time("timestamp"));
//if the post date is within 24 hours, return true
if(strtotime($thepost->post_date, current_time("timestamp")) >= $cutoff)
return true;
else
return false;
}
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Lock Posts to Members-Only After Specific Timeframe" at Paid Memberships Pro here: https://www.paidmembershipspro.com/lock-posts-members-specific-timeframe/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment