Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kimwhite/229538603fe2c5630dbbceb2d8df88f5 to your computer and use it in GitHub Desktop.
Save kimwhite/229538603fe2c5630dbbceb2d8df88f5 to your computer and use it in GitHub Desktop.
<?php // do not copy
/**
* This recipe will display a list of pages/posts that are protected by adding the shortcode `[mypmpro_display_posts]` to a page.
*
* 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 my_pmpro_display_posts_shortcode_filter( $args, $atts ) {
global $wpdb;
$sqlQuery = "SELECT page_id, membership_id FROM $wpdb->pmpro_memberships_pages GROUP BY page_id";
$results = $wpdb->get_results( $sqlQuery );
$allowed_post_types = array( 'post', 'page' ); // Add post types as necessary
$pmpro_posts = array();
foreach ( $results as $value ) {
$post = get_post( $value->page_id );
if ( $post && $post->post_status === 'publish' && in_array( $post->post_type, $allowed_post_types ) ) {
$pmpro_posts[] = array(
$post->post_title,
get_the_permalink( $value->page_id ),
$value->membership_id
);
}
}
$ret = "<ul>";
if ( ! empty( $pmpro_posts ) ) {
foreach ( $pmpro_posts as $pmp ) {
$ret .= "<li><a href='" . esc_url( $pmp[1] ) . "' target='_blank'>" . esc_html( $pmp[0] ) . "</a> [" . esc_html( $pmp[2] ) . "]</li>";
}
}
$ret .= "</ul>";
return $ret;
}
add_shortcode( 'mypmpro_display_posts', 'my_pmpro_display_posts_shortcode_filter' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment