0

I am currently listing posts based on user role, with the following query:

$ids = get_users( array('role' => 'author' ,'fields' => 'ID') );
$args = array(
        'author' => implode(',', $ids),
        'orderby' => 'date',
        'order' => 'ASC',
        );

Would it be possible to also limit the posts based on the membership level of the author using Paid Membership Pro? The plugin has the following hook to check user level:

if(pmpro_hasMembershipLevel($level_id))

But I'm not sure how to incorporate it into the above query (if possible)?

1
$ids = get_users( array('role' => 'author' ,'fields' => 'ID') );
$contr_limit    = count($ids);
for($cntr=0; $cntr < $contr_limit; $cntr++){
       if( pmpro_hasMembershipLevel($level_id, $ids[$cntr] ) !== true ){
            unset($ids[$cntr]);
       }
}

$args = array(
        'author' => implode(',', $ids),
        'orderby' => 'date',
        'order' => 'ASC',
        );
| improve this answer | |
  • Thanks for this - it doesn't seem to be working though and I'm not sure if I'm doing anything wrong. I've used the code as is, just changed $level_id to the actual ID of the membership level. Is there anything I need to do to get it to function correctly? At the moment, it still shows all posts by any membership level – Guit4eva Sep 1 '15 at 14:10
  • 1
    We have edited for $contr_limit variable, and also tested and it's working, check if the membership level id you have entered is proper and is linked with the users – WisdmLabs Sep 2 '15 at 6:14
  • I tried to get this working for over two hours - thenI realized I had a typo in my role ha ha - oh gracious. Thanks so much for this, it's working 100% :) :) – Guit4eva Sep 2 '15 at 13:39
1

Make sure your level ids are 1, 2, and 3, etc.

//For check level id 1.
if(pmpro_hasMembershipLevel(1, $user->ID))

You can check for get membership level of any user from documentation.

PMPro includes a built-in function allowing you to require membership or require a specific membership level.

| improve this answer | |
  • Thank you - but how would I include that into the query args? I'm not sure how to combine the two – Guit4eva Aug 6 '15 at 8:17
  • Not getting what you want explain. – AddWeb Solution Pvt Ltd Aug 6 '15 at 10:24
  • Would I include if(pmpro_hasMembershipLevel(1, $user->ID)) before if ( have_posts() )? – Guit4eva Aug 6 '15 at 12:01
  • You can use this like: if(pmpro_hasMembershipLevel(1, $user->ID)){ $args = array('post_type' => 'post', 'category_name' => $xyz);//Set as per your need $query = new WP_Query($args); if ($query->have_posts()) { //Your stuff.... } } Hope this help you. – AddWeb Solution Pvt Ltd Aug 6 '15 at 12:15
  • But would that not apply to the user viewing the content? I would like to show all posts by authors of a certain membership to all users – Guit4eva Aug 6 '15 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.