WordPress.org

Support

Support » WP-Advanced » Hide Categories from WordPress Dashboard by ID and Custom Post Type

Hide Categories from WordPress Dashboard by ID and Custom Post Type

  • HumbleHumans
    Member

    @humblehumans

    I recently went searching for a solution to hide specific categories by ID on a WordPress dashboard for individual custom post types. I stumbled upon the following code and was able to get half way there by hiding categories by logged in user.

    add_action( 'admin_init', 'wpse_55202_do_terms_exclusion' );
    
    function wpse_55202_do_terms_exclusion() {
        if( current_user_can('administrator')  )
            add_filter( 'list_terms_exclusions', 'wpse_55202_list_terms_exclusions', 10, 2 );
    }
    
    function wpse_55202_list_terms_exclusions($exclusions,$args) {
        return $exclusions . " AND ( t.term_id <> 43 )  AND ( t.term_id <> 42 )";
    }

    I now want to alter this code to hide ID specified categories taking the custom post type into account. I know i can pass an argument with the current_user_can but I’m not sure how to phrase it. Unfortunately a custom taxonomy which I have implemented in other aspects of the site is not going to work in this instance.

    I thought all I had to do was swap the if statement conditional with a get_post_type but it did not work. Any ideas?

    'andytoday' == get_post_type()

Viewing 4 replies - 1 through 4 (of 4 total)
  • Using your code, you could filter the post type like this:

    add_action( 'admin_init', 'wpse_55202_do_terms_exclusion' );
    function wpse_55202_do_terms_exclusion() {
        if( current_user_can('administrator')  )
            add_filter( 'list_terms_exclusions', 'wpse_55202_list_terms_exclusions', 10, 2 );
    }
    function wpse_55202_list_terms_exclusions($exclusions,$args) {
    	global $pagenow, $typenow;
    	if (in_array($pagenow,array('post.php','post-new.php')) && $typenow == 'your-post-type') {
    		$exclusions = " {$exclusions} AND ( t.term_id <> 43 )  AND ( t.term_id <> 42 )";
    	}
    	return $exclusions;
    }

    selnomeria
    Member

    @selnomeria

    What exzatly means hiding? Hiding from the “choose category” checkboxes menu(on “post editor” page)? If so, then you can use javascript too, to hide them. Something like this in functions.php:
    add_action("admin_menu","my_func"); function my_func(){ if (check_desired_condition){echo '<script>document.getElementById("fieldboxID").style.display="none;"</script>'; }}

    kaseban
    Participant

    @shandizstocks

    I want to hide a div from a my theme, when a stop word appear in title.

    <?php if (in_category(‘6’)) { ?>
    <div id=”adsense-up”>the adsens</div>
    <?php }else { ?>
    <?php } ?>
    +
    <div id=”adsense-bot”>the adsense</div>

    I want remove the two divs from the post when the certain words is in the title

    esmi
    Forum Moderator

    @esmi

    @shandiz: If you require assistance then, as per the Forum Welcome, please post your own topic.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Hide Categories from WordPress Dashboard by ID and Custom Post Type’ is closed to new replies.