Using membership to protect posts but still be able to show previews

I'm using membership plugin to protect blog posts on my site. Whilst this is working fine I have found that it completely hides the post so I can't display a title and excerpt on my homepage if i'm a free user. Is it at all possible to have this excerpt but when you click on the post it is then protected, mainly to encourage user signup ?

  • Jack Kitterhing
    • Code Norris

    Hi there Dave,

    Hope you're well today and thanks for your question.

    In this case, rather than using the post protection, I'd recommend using the more tag protection.

    So you'd add your content and then insert the more tag such as

    <!--more-->

    That will then allow you to show the content up to the more tag and the title to non members, if that would work for you?

    Thanks!

    Kind Regards
    Jack.

  • Dave
    • WPMU DEV Initiate

    That does work although I was also hoping to use membership to have different levels of access for different subscribers ? At the moment i'm just using the get_posts() which returns blank if not logged in, is there any way to get round this with the plugin ?

  • Dave
    • WPMU DEV Initiate

    I was hoping to just display a list of post excerpts on the homepage which you can then only click through to the main content if you're logged in. Problem is if I protect the posts using membership then I can't display the excerpts

  • aristath
    • Recruit

    Hello there @Dave,

    It looks to me like you'd have to write a custom template file to do that and then use some pretty fancy and custom code to directly get the content from the database (essentially bypassing WordPress functions) and then building the loop from scratch.

    However this is a little above our level of expertise so I have notified some of my more capable colleagues coding-wise on this thread, hopefully they'll be able to provide some custom code that will help you achieve what you need to do.

    Cheers,
    Ari.

  • Jose
    • Bruno Diaz

    Hi @Dave,

    Hope you are great today, and sorry for the huge delay on this.

    As you already noticed, the get_posts() method is returning blank when the user is not allowed to access that content. This is because the membership plugin is hooking in the pre_get_posts action, and therefore there is no way to avoid it. Even when suppress_filters is set to TRUE in your arguments array.

    So, as my colleague @aristath pointed, you need to use a custom query.
    The code would look something like the following:

    <?php
        $querystr = "
                   SELECT $wpdb->posts.*
                   FROM $wpdb->posts, $wpdb->postmeta
                   WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
                   ORDER BY $wpdb->posts.post_date ASC
                    ";
                $pageposts = $wpdb->get_results($querystr, OBJECT);
    
                ?>
              <?php if ($pageposts): ?>
                <?php global $post; ?>
                <?php foreach ($pageposts as $post): ?>
                   <?php setup_postdata($post); ?>
    
                   <div class="post" id="post-<?php the_ID(); ?>">
                   <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
                   <?php the_title(); ?></a></h2>
                   <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
                   <div class="entry">
                       <?php the_excerpt('Read the rest of this entry »'); ?>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php else : ?>
           <h2 class="center">Not Found</h2>
        <p class="center">Sorry, but you are looking for something that isn't here.</p>
    <?php endif; ?>

    You should modify the SELECT criteria to match your needs. Here you can find the detailed explanation: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

    Please let me know if this was useful for you.

    Cheers!
    JJ

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.