• Resolved bcourcet

    (@bcourcet)


    Hello,

    how to hide (not only restrict) posts from listings and search ?

    (i don’t mind buying the paid version)

    Best regards

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Caseproof

    (@caseproof)

    Hi @bcourcet

    We don’t have any option to hide protected posts on a listing page like Blog page. You can try to use this code snippet to exclude those posts on Blog page:

    add_action( 'pre_get_posts', function ( $query ) {
    	if ( ! is_admin() && $query->is_main_query() && 'blog' === $query->query['pagename'] ) {
    		$posts = get_posts( array( 'post_type' => 'post', 'post_status' => 'publish', 'numberposts' => - 1 ) );
    		if ( empty( $posts ) ) {
    			return;
    		}
    		$protected_posts = array();
    
    		foreach ( $posts as $post ) {
    			if ( ! members_can_current_user_view_post( $post->ID ) ) {
    				$protected_posts[] = $post->ID;
    			}
    		}
    		if ( empty( $protected_posts ) ) {
    			return;
    		}
    
    		$query->set( 'post__not_in', $protected_posts );
    	}
    } );

    To hide posts from search results, you can try this plugin: https://www.ads-software.com/plugins/mpress-hide-from-search/

    I hope that helps.

    Thread Starter bcourcet

    (@bcourcet)

    Hello,

    and thank you for the quick reply. But may i ask you what do you call a “protected post” ?

    Thank you

    Plugin Author Caseproof

    (@caseproof)

    By protected posts I mean posts protected with Content Permissions feature in Members plugin: https://members-plugin.com/docs/content-permissions/

    When you protect posts this way, their content is protected, but they are still being displayed in the posts listing on Blog page. The method I sent you allows you to override it and remove those posts from listing page.

    I hope that makes sense.

    vivigonzab

    (@vivigonzab)

    Hi! Thanks for that code snippet.

    I was wondering if it will work for another page (not the blog page).

    I wanted to create a new Page “restricted content” where to display the posts from an specific category.

    So you know what should I modify in the code snippet in order to work?

    Thank you very much!

    vivigonzab

    (@vivigonzab)

    Hi @caseproof !! do you think could yo help me with this please?

    Thanks a lot!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Hide content, not restrict’ is closed to new replies.