Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save greathmaster/ef0f79c5297dc4a32c94 to your computer and use it in GitHub Desktop.
Save greathmaster/ef0f79c5297dc4a32c94 to your computer and use it in GitHub Desktop.
Displays membership level ids a post is assigned to under the manage posts page.
function my_pmpro_post_membership_levels_header($defaults)
{
$defaults['membership_levels'] = 'Membership Levels';
return $defaults;
}
function my_pmpro_post_membership_levels($column_name, $post_ID)
{
if ($column_name == 'membership_levels')
{
foreach(my_pmpro_has_membership_access($post_ID) as $level_id)
echo $level_id." ";
}
}
function my_pmpro_has_membership_access($post_id)
{
global $wpdb;
//use globals if no values supplied
//no post, return true (changed from false in version 1.7.2)
if(!$post_id)
return false;
$mypost = get_post($post_id);
if($mypost->post_type == "post")
{
$post_categories = wp_get_post_categories($mypost->ID);
if(!$post_categories)
{
//just check for entries in the memberships_pages table
$sqlQuery = "SELECT m.id, m.name FROM $wpdb->pmpro_memberships_pages mp LEFT JOIN $wpdb->pmpro_membership_levels m ON mp.membership_id = m.id WHERE mp.page_id = '" . $mypost->ID . "'";
}
else
{
//are any of the post categories associated with membership levels? also check the memberships_pages table
$sqlQuery = "(SELECT m.id, m.name FROM $wpdb->pmpro_memberships_categories mc LEFT JOIN $wpdb->pmpro_membership_levels m ON mc.membership_id = m.id WHERE mc.category_id IN(" . implode(",", $post_categories) . ") AND m.id IS NOT NULL) UNION (SELECT m.id, m.name FROM $wpdb->pmpro_memberships_pages mp LEFT JOIN $wpdb->pmpro_membership_levels m ON mp.membership_id = m.id WHERE mp.page_id = '" . $mypost->ID . "')";
}
}
else
{
//are any membership levels associated with this page?
$sqlQuery = "SELECT m.id, m.name FROM $wpdb->pmpro_memberships_pages mp LEFT JOIN $wpdb->pmpro_membership_levels m ON mp.membership_id = m.id WHERE mp.page_id = '" . $mypost->ID . "'";
}
$post_membership_levels = $wpdb->get_results($sqlQuery);
$post_membership_levels_ids = array();
$post_membership_levels_names = array();
if(!$post_membership_levels)
{
return false;
}
else
{
foreach($post_membership_levels as $level)
{
$post_membership_levels_ids[] = $level->id;
//$post_membership_levels_names[] = $level->name;
}
return $post_membership_levels_ids;
}
}
add_filter('manage_posts_columns', 'my_pmpro_post_membership_levels_header');
add_action('manage_posts_custom_column', 'my_pmpro_post_membership_levels', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment