This topic has 3 replies, 2 voices, and was last updated 8 years by Radu.

  • Author
  • #146485
     proteus
    Participant

    Hello.

    As the title suggests, is there any way to exclude an entire category and any posts under that category from the ajax search function if the current user has a certain role ‘testman’.

    Kind Regards

    #146587
     Radu
    Moderator

    Hi,

    Add this function to wp-content/themes/buddyapp-child/functions.php

    COPY CODE
    
    
    function sq7_altering_ajax_search($defaults){
    
        $defaults['cat'] = '-20';
    
    }
    apply_filters( 'kleo_ajax_query_args', 'sq7_altering_ajax_search');
    

    20 represents category ID and minus sign represents remove.

    Full documentation to WP Query args
    https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

    Cheers
    R.

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution
    #146677
     proteus
    Participant

    Hello I tried this and it did not work. The results in the top search bar still return a list of posts from the category that I do not want visible.

    Below is what I have added to the child theme functions.php file.

    I have attached an image.

    COPY CODE
    /**
    	* EXCLUDE INTRAHUB CATEGORY FROM SEARCH AJAX EXCEPT FOR USERS THAT ARE ADMINS OR PROTEUS
    */ 
    
    function sq7_altering_ajax_search($defaults){
    	if ( current_user_can('proteus') || ! $query->is_main_query() ) {
    		return;
    	} else { 
    		$defaults['cat'] = '-1230';
    	}
    }
    
    apply_filters( 'kleo_ajax_query_args', 'sq7_altering_ajax_search');
    
    /**
    	* EXCLUDE INTRAHUB CATEGORY FROM SEARCH EXCEPT FOR USERS THAT ARE ADMINS OR PROTEUS
    */ 
    function wcs_exclude_category_search( $query ) {
      if ( current_user_can('proteus') || ! $query->is_main_query() )
        return;
    
      if ( $query->is_search ) {
        $query->set( 'cat', '-1230' );
      }
    
    }
    Attachments:
    You must be logged in to view attached files.
    #146788
     Radu
    Moderator

    Hi,

    Use this snippet instead, it should work just i’ve tested

    COPY CODE
    
    function sq7_altering_ajax_search($defaults){
        $defaults['cat'] = '-20';
        return $defaults;
    }
    add_filter( 'kleo_ajax_query_args', 'sq7_altering_ajax_search');
    
    

    Cheers
    R

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution
Viewing 4 posts - 1 through 4 (of 4 total)

The forum ‘General questions’ is closed to new topics and replies.

Log in with your credentials

Forgot your details?