[resolved] Always force one specific category (6 posts)

  1. applegateian
    Posted 3 years ago #

    I've seen a few plugins that allow you to force at leaste one category for every user. However, what I want to do is ALWAYS force the default category on the user without them being able to remove it.

    They can select other categories additionally, but the default category must always be selected. Is there a way to do this?

    I am using the Restrict Categories plugin to limit the categories to my different user types, but I want them all to be forced to use the default...I cannot find a way to do this.



  2. applegateian
    Posted 3 years ago #

    Any thoughts on this guys?

  3. keesiemeijer
    Posted 3 years ago #

    Try it with this in your theme's functions.php:

    function set_my_categories( $post_ID ) {
    	$default_cat_id = 1; // change this to the category ID you want
    	if ( wp_is_post_autosave( $post_ID ) || wp_is_post_revision( $post_ID ) ) {
    		return $post_ID;
    	$term_list = wp_get_post_terms( $post_ID, 'category', array( "fields" => "ids" ) );
    	if ( $term_list ) {
    		$term_list[] = $default_cat_id ;
    		wp_set_post_categories( $post_ID, array_unique( $term_list ) );
    	} else {
    		wp_set_post_categories( $post_ID, array( $default_cat_id ) );
    add_action( 'save_post', 'set_my_categories' );

    This will automatically assign the category with ID 1 when a post is published or updated

  4. applegateian
    Posted 3 years ago #

    Thanks for the suggestion, currently getting this error with this code:

    Parse error: syntax error, unexpected T_IF in /nfs/c08/h03/mnt/126187/domains/architectsrepublic.com/html/community/wp-content/themes/reverie/functions.php on line 176

  5. keesiemeijer
    Posted 3 years ago #

    I just copy pasted it in my theme's functions.php and get no errors.

    Can you post the full code of your functions.php file with my code in it. See: http://codex.wordpress.org/Forum_Welcome#Posting_Code

  6. applegateian
    Posted 3 years ago #

    My mistake - pasted incorrectly and it works PERFECTLY. The checkbox is there by default, that will do the job. Thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic