Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Mikko Saari

    (@msaari)

    An indexing filter does not work, because that hits all users equally. You need the products in the index to be able to show them to some users.

    The tool you need is relevanssi_post_ok, which can be used control who sees what. You have the post ID as a parameter, so you can check the category, and you can get the user ID from wp_get_current_user() and then just return false if the category and the user match.

    Thread Starter eddiebouncer

    (@eddiebouncer)

    Thanks for pointing me in the right direction,

    Do i need the post ID? as the filter is based on the restricted (download)category (Easy Digital Downloads) and specific user

    I fiddled some, but it doesn’t work – and doesn’t feel ok either :/

    add_filter( 'relevanssi_post_ok', 'rlv_restricted', 10, 2 );
    
    function rlv_restricted( $post_ok, $post_id, $current_user_id, $download_category ) {
    
    	$current_user_id = wp_get_current_user();
    	$post = get_post( $post_id );
    	
    	if ( $current_user_id = 10 && $download_category = 795 ) {
    		$post_ok = false;
    	}
    	else {
    
    	return $post_ok;
    }
    Plugin Author Mikko Saari

    (@msaari)

    Yes, you need the post ID to check if the post has the category you want to restrict. You don’t have to get the post object with get_post(). You can use has_term() to check if the post has the restricted term.

    Thread Starter eddiebouncer

    (@eddiebouncer)

    Hmm, that’s too indepth relevanssi for me.

    I’ve tried to exclude the restricted categories for the specific user from the search results:

    function my_search_filter( $query ) {
        $user_ID = get_current_user_id(); 
    	global $wp_the_query;
    
    	if( $query === $wp_the_query && $query->is_search() && $user_ID == 10 ) {
    	$tax_query = array(
    		array(
    			'taxonomy' => 'download_category',
    			'field' => 'slug',
    			'terms' => 'hidden-cat',
    			'operator' => 'NOT IN',
    			)
    		);
    	$query->set( 'tax_query', $tax_query );
    		}
    	}
    
    add_action( 'pre_get_posts', 'my_search_filter' );

    Seems to work alright – or would this be a weird workaround?

    Cheers, Ed’

    Plugin Author Mikko Saari

    (@msaari)

    That works. It isn’t weird at all, it’s a fine solution. I was thinking of something like this:

    add_filter( 'relevanssi_post_ok', 'rlv_restricted', 10, 2 );
    function rlv_restricted( $post_ok, $post_id ) {
      $current_user = wp_get_current_user();
      if ( 10 === $current_user->ID && has_term( 'hidden-cat', 'download_category', $post_id ) ) {
        $post_ok = false;
      }
      return $post_ok;
    }
    Thread Starter eddiebouncer

    (@eddiebouncer)

    Thanks for thinking along. I like ur snippet as it’s more Relevanssi specific – where mine is more ‘in general’

    Cheers, Ed’

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘exclude specific category for specific user’ is closed to new replies.