Membership plugin missing support for custom post types

Is it possible to use custom post types for the membership plugin?
The custom post types are automaticly hidden for everyone and I can't change them any where.

  • DavidM

    Hello dinwebb,

    First of all, welcome to WPMU Dev! We're glad to have you here!

    It is indeed possible to use custom post types alongside our Membership plugin and in fact, our CustomPress plugin can help you create these custom post types, if you haven't already tried it.

    https://premium.wpmudev.org/project/custompress

    That said, have you tried seeing if the custom post types you're using are available without the Membership plugin active? I realize if it's in a live setting, it may not be viable but is there some way you could check that the custom post types are capable of being displayed somehow? Rather, are these posts displaying when you're logged in as admin?

    On another note, I've had the opposite results as you've had here since Membership doesn't yet have a rule to restrict access to custom post types. So the custom post types are visible to the public.

    Cheers,
    David

  • dinwebb

    Thanks David

    The post_type were only hidden if i had put restrictions on posts and I also tried to query the post type on a page but id didn't work at all.

    I found the problem inside 'includes/default.rules.php'.
    The functions: 'add_viewable_pages, add_viewable_posts, add_unviewable_pages, add_unviewable_posts' were checking if(is_page()) return; and if(!is_page()) return;
    I changed the if statement to:
    if($wp_query->query_vars['post_type'] != 'page') {
    return;
    }
    and
    if($wp_query->query_vars['post_type'] != 'post') {
    return;
    }
    This solved everything and then I extended 'M_Rule' for my post type.

    The only problem I didn't find a solution to was how to loop through all public none _builtin post_types and create a class for each

    /Spathon

  • dinwebb

    there should be support for custom taxonomy to.

    Here is an manual solution to custom taxonomy (wordpress 3.1+):
    copy the class rule for categories and change it according to your taxonomy.
    $categories = get_categories('get=all&taxonomy=your_tax');

    Ex on include function

    function add_viewable_posts($wp_query) {
    	if($wp_query->query_vars['post_type'] != 'supported_post_type') {
    		return;
    	}
    	$c_id = array();
    	foreach( (array) $this->data as $key => $value ) {
    		$c_id[] = $value;
    	}
    	$wp_query->query_vars['tax_query'] = array(
    			'relation' => 'IN',
    			array(
    				'taxonomy' => 'your_tax',
    				'terms' => $c_id,
    				'field' => 'id',
    			)
    	);
    }
  • dinwebb

    Hi David,

    I created a new file in the plugins folder and copied rules for posts and modified it to my post type(ps_media).

    class M_Ps_Media extends M_Rule {
    
    	var $name = 'ps_media';
    	var $label = 'Media';
    
    	function admin_main($data) {
    		if(!$data) $data = array();
    		?>
    		<div class='level-operation' id='main-ps_media'>
    			<h2 class='sidebar-name'><?php _e('Media', 'membership');?><span><a href='#remove' id='remove-ps_media' class='removelink' title='<?php _e("Remove Posts from this rules area.",'membership'); ?>'><?php _e('Remove','membership'); ?></a></span></h2>
    			<div class='inner-operation'>
    				<p><?php _e('Select the posts to be covered by this rule by checking the box next to the relevant posts title.','membership'); ?></p>
    				<?php
    					$args = array(
    						'numberposts' => MEMBERSHIP_POST_COUNT,
    						'offset' => 0,
    						'orderby' => 'post_date',
    						'order' => 'DESC',
    						'post_type' => 'ps_media',
    						'post_status' => 'publish'
    					);
    
    					$posts = get_posts($args);
    					if($posts) {
    						?>
    						<table cellspacing="0" class="widefat fixed">
    							<thead>
    							<tr>
    								<th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th>
    								<th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Post title', 'membership'); ?></th>
    								<th style="" class="manage-column column-date" id="date" scope="col"><?php _e('Post date', 'membership'); ?></th>
    							</tr>
    							</thead>
    
    							<tfoot>
    							<tr>
    								<th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th>
    								<th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Post title', 'membership'); ?></th>
    								<th style="" class="manage-column column-date" id="date" scope="col"><?php _e('Post date', 'membership'); ?></th>
    							</tr>
    							</tfoot>
    
    							<tbody>
    						<?php
    						foreach($posts as $key => $post) {
    							?>
    							<tr valign="middle" class="alternate" id="post-<?php echo $post->ID; ?>">
    								<th class="check-column" scope="row">
    									<input type="checkbox" value="<?php echo $post->ID; ?>" name="ps_media[]" <?php if(in_array($post->ID, $data)) echo 'checked="checked"'; ?>>
    								</th>
    								<td class="column-name">
    									<strong><?php echo esc_html($post->post_title); ?></strong>
    								</td>
    								<td class="column-date">
    									<?php
    										echo date("Y/m/d", strtotime($post->post_date));
    									?>
    								</td>
    							</tr>
    							<?php
    						}
    						?>
    							</tbody>
    						</table>
    						<?php
    					}
    
    				?>
    				<p class='description'><?php echo sprintf(__("Only the most recent %d posts are shown above, if you have more than that then you should consider using categories instead.",'membership'), MEMBERSHIP_POST_COUNT); ?></p>
    			</div>
    		</div>
    		<?php
    	}
    
    	function on_positive($data) {
    
    		$this->data = $data;
    
    		add_action('pre_get_posts', array(&$this, 'add_viewable_pages'), 1 );
    
    	}
    
    	function on_negative($data) {
    
    		$this->data = $data;
    
    		add_action('pre_get_posts', array(&$this, 'add_unviewable_pages'), 1 );
    	}
    
    	function add_viewable_pages($wp_query) {
    
    		if($wp_query->query_vars['post_type'] != 'ps_media') {
    			return;
    		}
    
    		foreach( (array) $this->data as $key => $value ) {
    			$wp_query->query_vars['post__in'][] = $value;
    		}
    
    		$wp_query->query_vars['post__in'] = array_unique($wp_query->query_vars['post__in']);
    
    	}
    
    	function add_unviewable_pages($wp_query) {
    
    		if($wp_query->query_vars['post_type'] != 'ps_media') {
    			return;
    		}
    
    		foreach( (array) $this->data as $key => $value ) {
    			$wp_query->query_vars['post__not_in'][] = $value;
    		}
    
    		$wp_query->query_vars['post__not_in'] = array_unique($wp_query->query_vars['post__not_in']);
    
    	}
    
    }
    M_register_rule('ps_media', 'M_Ps_Media', 'main');

    Hope It will be any help

    /Spathon

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.