WordPress.org

Ready to get started?Download WordPress

Forums

Hide Categories from WordPress Dashboard by ID and Custom Post Type (5 posts)

  1. HumbleHumans
    Member
    Posted 1 year ago #

    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()

  2. David Beja
    Member
    Posted 1 year ago #

    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;
    }
  3. selnomeria
    Member
    Posted 6 months ago #

    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>'; }}

  4. shandiz
    Member
    Posted 6 months ago #

    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

  5. esmi
    Forum Moderator
    Posted 6 months ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic