Hello. Thank you for using Paid Memberships Pro. I can outline a way to do this. You would hook into the profile page, and check his level using the function pmpro_hasMembershipLevel()
and the seeing which categories that level has access to by using pmpro_getMembershipCategories()
and then displaying them. Does that make sense? Let us know if you have any questions or concerns. Thanks again.
With a help of others I’ve come up with the following code:
It returns a link to a post, but only when a user has an access to one them. If he has access to more than one, then it doesn’t work (returns blank).
<?php
$sanitized_title = sanitize_title($current_user->membership_level->name);
if( $sanitized_title ) {
$args = array('post_type' => 'post', 'category_name' => $sanitized_title);
$query = new WP_Query( $args );
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
}
wp_reset_postdata();
}
}
?>
I’m a beginner in PHP and WordPress coding, but I will do my best to experiment with the functions provided. Nevertheless If you could share any more details, that would be helpful. Probably someone else would benefit of that too. Thanks for your support.
All the best
Tom
What should I pass as a parameter to the function pmpro_getMembershipCategories()?
function pmpro_getMembershipCategories($level_id)
Currently when I pass pmpro_getMembershipCategories($current_user->ID);
I get:
array(1) {
[0]=>
string(2) "14"
}
Is that OK?
Then it outputs
Warning: urlencode() expects parameter 1 to be string, array given in /home/e7066/domains/inwima.pl/public_html/wp-includes/formatting.php on line 3690
And links to all posts in all the categories, not just the member’s.
The whole function is:
<?php
$member_level = pmpro_hasMembershipLevel('$current_user->membership_levels', '$current_user->ID');
echo '<pre>';
var_dump($member_level);
echo '</pre>';
$sanitized_title = pmpro_getMembershipCategories($current_user->ID);
echo '<pre>';
var_dump($sanitized_title);
echo '</pre>';
if ($sanitized_title) {
$args = array('post_type' => 'post', 'category_name' => $sanitized_title);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
}
wp_reset_postdata();
}
}
?>
My goal:
According to how the plugin works, a member has to unlock his access to categories in order to get to a post. I have in each category one post, because what I really want is to have unlockable posts. (BTW the way how it works now is OK, I don’t need to change that).
However, I need to display on a member profile page all the posts he had unlocked (and links to them, I would like to be able to style them too). This means I need to get inside member’s categories and loop through them.