show protected posts in blog archive

I would like users of a site to see a preview of the content (i.e. show the archive pages with all the posts) and then when they click to view a post they will be restricted and redirected to the protected content page ...

Currently no protected posts show on archive pages so it looks as if there is no content.

  • Milan

    Hello @timvb,

    I hope you are having a good day and thanks for the posting. :slight_smile:

    By default Membership 2 pro hides protected posts from blog archive page, and you can't actually configure this behaviour.

    But however there is one workaround which you can use to let display protected posts on blog archive page. You can create shortcode which will render archive page post lists and then you can inject that shortcode to particular page, finally you need to make that page as blog listing archive page.

    Here is sample code with steps you can use to understand solution better.

    #1) Paste this shortcode code snippet in your current theme's functions.php file, or better you can create child theme or stand-alone plugin for this too.

    add_shortcode( 'wpmu_blog_archive', 'wpmu_blog_archive' );
    
    function wpmu_blog_archive( $atts, $content = '' ){
    	$Query = new WP_Query(array('post_type' => 'post'));
    
    	$html .= '';
    
    	if( $Query->have_posts() ){
    		while( $Query->have_posts() ){
    			$Query->the_post();
    
    			$html .= '<div>';
    			$html .= '<h1>' . get_the_title() . '</h1>';
    			$html .= '<p>' . get_the_excerpt() . '</p>';
    
    			$html .= &quot;<br><br><br><br><br><br><br>&quot;;
    			$html .= '</div>';
    		}
    	}
    
    	wp_reset_query();
    
    	return $html;
    }

    #2) Create one page, give it title like "Blog Listing".
    #3) Paste below shortcode tag in content area of "blog listing" page.

    [wpmu_blog_archive]

    #4) Publish the page.
    #5) Head over to Settings > Reading > Front-page displays., Select "Blog Listing" as posts page.
    #6) Save changes. View blog listing page in browser. I am sure you will get all the protected posts as lit.

    That's it!. Now You can list all the protected posts with the help of above shortcode, if you new to shortcode mechanism of wordpress, i suggest you read this nice article,

    http://www.smashingmagazine.com/2012/05/wordpress-shortcodes-complete-guide/

    And you can always change above shortcode's html structure according to your taste. :slight_smile:

    I hope this helps you.

    Please let me know if there is anything else I can help you with.

    Best Regards,
    Milan Savaliya.

  • timvb

    @Milan

    I have another site with the previous version of the membership plugin and it had this behaviour ... it's the ideal implementation for a membership site as it allows users to see what they're getting access to before they sign up ..

    the theme on our site has a number of different blog archive layouts which also get placed on our homepage in blocks .... how can I keep those layouts and use your filter on these to allow posts to show ..

    UPDATE: i pasted that into my child theme functions.php and it crashed the site .. i had to remove it.

  • Graham

    Just for anyone coming across this post later (appreciate it's pretty old)... the shortcode code does actually have a mistake in it (missing quotes), see below for a working version to add into functions.php / child theme / plugin:

    add_shortcode( 'wpmu_blog_archive', 'wpmu_blog_archive' );
    
    function wpmu_blog_archive( $atts, $content = '' ){
    	$Query = new WP_Query(array('post_type' => 'post'));
    
    	$html .= '';
    
    	if( $Query->have_posts() ){
    		while( $Query->have_posts() ){
    			$Query->the_post();
    
    			$html .= '<div>';
    			$html .= '<h1>' . get_the_title() . '</h1>';
    			$html .= '<p>' . get_the_excerpt() . '</p>';
    
    			$html .= '<br><br><br><br><br><br><br>';
    			$html .= '</div>';
    		}
    	}
    
    	wp_reset_query();
    
    	return $html;
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.