Skip to content

Instantly share code, notes, and snippets.

@contemplate
Created February 8, 2024 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save contemplate/dbc005ee8093acb00575757de3080551 to your computer and use it in GitHub Desktop.
Save contemplate/dbc005ee8093acb00575757de3080551 to your computer and use it in GitHub Desktop.
WooCommerce Memberships: exclude post loops on forced public pages
// Initialize the global flag on the wp action hook
add_action('wp', 'initialize_force_public_page_flag');
function initialize_force_public_page_flag() {
global $is_force_public_page, $wp_query;
$is_force_public_page = false;
if (!empty($wp_query->queried_object_id)) {
$is_force_public_page = get_post_meta($wp_query->queried_object_id, '_wc_memberships_force_public', true) === 'yes';
}
}
// Filter to make posts public for pages with _wc_memberships_force_public set to 'yes'
add_filter('wc_memberships_is_post_public', 'make_posts_public_for_force_public_page', 10, 3);
function make_posts_public_for_force_public_page($is_public, $post_id, $post_type) {
global $is_force_public_page;
// Check if we're on a page with the _wc_memberships_force_public key set to 'yes'
if ($is_force_public_page) {
// If so, make all posts appear as public
return true;
}
return $is_public;
}
@contemplate
Copy link
Author

This snippet is helpful if you are wanting to show a preview of member post content in a post gallery or other custom loop on a public page. Simply enable the "Disable restrictions" on the page where you will be showing the custom member post loops and then will fully appear.

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