• Resolved owhited

    (@owhited)


    I am using pre_get_terms hook to hide certain terms (Category and tags) from the user. It works as intended in the term creation page edit-tags.php.

    But when I go to the post creation page post.php, and look at the “categories” section on the right, where the terms are fetched through ajax, I can see ALL terms.

    Why does the hook work in one page and not another?
    Does ajax have anything to do with it?

Viewing 3 replies - 1 through 3 (of 3 total)
  • You can try to use the list_terms_exclusions hook. Naeem Musa suggested in his answer on WPSE:

    /**
     * Hide terms from being listed in the list categories metabox
     *
     * @param String $exclusions
     * @param Array $args
     *
     * @return String $exclusions
     */
    function hide_categories_for_specific_user( $exclusions, $args ) {
    	
    	// Term IDs to be excluded
    	$exclude = array( 1, 2, 3 );
    	
    	foreach( $exclude as $term_id ) {
    		
    		if( ! empty( $exclusions ) ) {
    			$exclusions .= ' AND t.term_id <> ' . $term_id . ' ';
    		} else {
    			$exclusions = ' AND ( t.term_id <> ' . $term_id . ' ';
    		}
    		
    	}
    
    	// Closing bracket
    	if( ! empty( $exclusions ) ) {
    		$exclusions .= ')';
    	}
    
    	// Return our SQL statement
    	return $exclusions;
    	
    }
    add_filter( 'list_terms_exclusions', 'hide_categories_for_specific_user', 10, 2 );
    Thread Starter owhited

    (@owhited)

    Term exclusion works in posts.php page. ??
    Why does pre_get_terms hook work only in edit-tags.php page and not in posts.php?

    Thread Starter owhited

    (@owhited)

    if ( (! $am_i_admin) && is_admin() ) {
    When I used the above condition, it targeted only the edit-tags.php page’s term listing. post.php is not affected. Though both are admin pages. Maybe because post.php uses ajax to get the terms.

    if ( (! $am_i_admin) ) {
    When I used the above condition, it targeted both pages as intended.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘hook works in one page but not another’ is closed to new replies.