Membership 2 Pro protected content

I want to output a post by custom query while the post is protected by Membership 2 Pro. But unable to do it. Is there any way to get around it? I want to show the post in the search.

  • Dimitris

    Hello there fonderco,

    hope you're doing well today!

    We removed protected post via the pre_get_posts hook, so you can check the following code in this MU Plugin, which is used for showing protected posts in archives: https://gist.github.com/wpmudev-sls/8b7881f0c17f9b481c74462cc05c217a

    If you're finding troubles with it, please do share your custom code so we can have a better picture. As our support forums are publicly available, if you don't want to do that publicly, just let us know here in your next reply. :slight_smile:

    Warm regards,
    Dimitris

  • fonderco

    I see how that code removes the pre_get_posts filter, but I don't want to remove that filter permanently. Only inside of another plugin.

    I'm using the plugin WP Courses (https://wordpress.org/plugins/wp-courses/) as I need a basic course taking system. I would CoursePress but it is just too detailed. Each of my courses consists of a single video. In CoursePress I have to make a unit and a module and just through 10 hoops to create a basic course that only has a single lesson.

    Anyway...

    WP Courses has 2 custom post types. "courses" which basically allow the user to see an overview and "lessons" which are the actual content.

    In Membership2, I have protected the "lesson" custom post type.

    Now to the issue.

    Lessons have a custom field with the ID of the course they are associated with. So, on the single course page to know which lesson to start them on... The plugin uses WP_Query to find the lesson that has the custom field equal to the course's post_id.

    Because lessons are protected this query is always empty.

    $args = array(
    'post_type' => 'lesson',
    'meta_value' => $course_id,
    'meta_key' => 'wpc-connected-lesson-to-course',
    'posts_per_page' => 1,
    'paged' => false,
    'nopaging' => true,
    'order' => 'ASC',
    'orderby' => 'menu_order'
    );

    $query = new WP_Query($args);

    if($query->have_posts()){
    while($query->have_posts()){
    $query->the_post();
    $course_link = get_the_permalink();
    break;
    }
    }

    How to replicate this issue:

    1. Install Membership2 and create a membership
    2. Install WP Courses and create a course with a lesson
    3. Be logged out and view the course and then click "Start" to start the lesson
    4. You will be taken to register, don't register
    5. Now protect the "lesson" custom post type
    6. Be logged out and view the course and then click the "Start" button - it doesn't take you anywhere because it doesn't know the lesson to send you to

  • Tho Bui

    Hi fonderco,

    I hope you're well today!

    I have prepared a custom snippet which you can use as a guide here :
    https://gist.github.com/wpmudev-sls/6aaac87bedd770711fe1099d9ad23855

    You can download a zip, unzip it and upload file m2p-wp-course-fix-issue-connect-lesson.php to your wp-content/mu-plugins folder. If that folder doesn't exist you can simply create it.

    If you are not familiar with mu-plugins you can read about them here:
    https://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/
    or you can paste it in your child theme's functions.php file.

    Kind regards,
    Tho Bui

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.