• Resolved katie

    (@katiehartrick)


    I have several custom post types and want to show/ hide restricted pages based on post type.

    I know I can set “Filter Searches and Archives” to either yes or no. On this site, I want to be able to show posts and pages, while hiding them on custom post types.

    What is the best way to achieve this?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter katie

    (@katiehartrick)

    In settings i’ve set Filter Searches and Archives to No. My custom post type is called “stories”.

    Looks like I need to add a filter to amend this existing function from paid-memberships-pro/includes/content.php

    $pmpro_search_filter_post_types = apply_filters( 'pmpro_search_filter_post_types', array( 'page', 'post' ) );
    if ( ! is_array( $pmpro_search_filter_post_types ) ) {
    	$pmpro_search_filter_post_types = array( $pmpro_search_filter_post_types );		
    }
    Thread Starter katie

    (@katiehartrick)

    I’ve tried adding this function to child theme functions.php, the locked stories posts are still showing when user is locked out.

    function pmpro_hide_stories( $post_types ) {
        $post_types[] = 'stories';
        return $post_types;
    }
    add_filter('pmpro_search_filter_post_types', 'pmpro_hide_stories');
    Thread Starter katie

    (@katiehartrick)

    amendment – that snippet did work. Issue closed

    Plugin Author Andrew Lima

    (@andrewza)

    Thanks for letting me know @katiehartrick, glad to hear you have figured this out.

    Thread Starter katie

    (@katiehartrick)

    @andrewza I need to reopen this, during testing I switched “Filter searches and archives?” to yes. I’ve switched it back to no and the stories CPT is showing in search results.

    Related question – is there a way to exclude member pages using a WordPress query? To date, my solution is tagging non-member pages with a specific category and filtering the query using that category.

    • This reply was modified 3 years, 7 months ago by katie.
    Plugin Author Andrew Lima

    (@andrewza)

    Sure that’s no problem at all. To confirm, you want to exclude post categories from this filtering when searching?

    So for example, restricted posts that have category “X” will show, while restricted posts with category “Y” will be hidden?

    Thread Starter katie

    (@katiehartrick)

    There seems to be a misunderstanding. I have already created a CPT query and filtered it to show a set category.

    
    $args = array(
     'category_name' => 'public',
     'order'    => 'title',
     'post_type'      => 'stories',
     'posts_per_page' => -1
    );

    This relies on web authors setting all public posts to a specific category. My question is, is there a way to filter the query to specifically hide PMPro membership posts? Or is the only way to hide PMPro membership posts from the query is to ensure all public posts have a specific category set (in this example public)

    I’ve read using post__not_in leads to performance issues, and setting a category to exclude means setting another parameter in addition to setting PMPro membership.

    Plugin Author Andrew Lima

    (@andrewza)

    Did the snippet mentioned earlier on to add your CPT to the filter for searches and archives? Is it not hiding the content once you’ve added the snippet previously mentioned. The built in filter Paid Memberships Pro does, includes categories as well as any posts that have the “Require Membership” options selected and hides it accordingly – https://www.paidmembershipspro.com/filtering-members-only-content-in-home-archives-searches-and-widgets/

    We do try to keep this as optimized as possible.

    I recommend reaching out to a local WordPress developer to assist you with writing the optimized solution here.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Restrict content based on post type’ is closed to new replies.