Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kimwhite/a8b93e8d544dba9d7197bd35f8316c1e to your computer and use it in GitHub Desktop.
Save kimwhite/a8b93e8d544dba9d7197bd35f8316c1e to your computer and use it in GitHub Desktop.
Shortcode to display pages a current member has access to...see comments and notes for details.
<?php // do not copy this line.
/**
* Shortcode [member_content_list] to display pages a current member has access to.
* Only shows those pages with explicit membership settings. ie those set with the
* "Require Membership" box on pages, CPTs (if the PMPro CPTs plugin is used), and posts.
* It does not show those posts restricted by categories.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function member_content_list($atts)
{
$args = shortcode_atts(array('post_type' => 'page'), $atts);
$post_type = $args['post_type'];
global $wpdb;
$level = pmpro_getMembershipLevelForUser();
$sql = "SELECT * FROM $wpdb->pmpro_memberships_pages LEFT JOIN $wpdb->posts ON $wpdb->pmpro_memberships_pages.page_id = $wpdb->posts.ID WHERE post_type = '$post_type' AND $wpdb->pmpro_memberships_pages.membership_id = $level->id";
$results = $wpdb->get_results($sql, OBJECT);
ob_start();
foreach ($results as $result)
{?>
<li><a href="<?php echo esc_url( get_permalink($result->page_id) ); ?>"><?php esc_html_e($result->post_title); ?></a></li><?php
}
$list = ob_get_contents();
ob_end_clean();
return $list;
}
add_shortcode('member_content_list' , 'member_content_list');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment