Last active
March 28, 2025 16:51
-
-
Save JarrydLong/c8187050b72db19bae3b19ec6c5bc6e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 $key => $value ) { | |
$post = get_post( $value->page_id ); | |
if( $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='".$pmp[0]."' target='_BLANK'>".$pmp[1]."</a> [".$pmp[2]."]</li>"; | |
} | |
} | |
$ret .= "</ul>"; | |
return $ret; | |
} | |
add_shortcode( 'mypmpro_display_posts', 'my_pmpro_display_posts_shortcode_filter', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment