Forums

Set category to a custom post type automatically extended (1 post)

  1. baszer
    Member
    Posted 6 months ago #

    here I found the following code to auto select a checkbox for my custom post.

    function add_housecategory_automatically($post_ID) {
    	global $wpdb;
    	if(!wp_is_post_revision($post_ID)) {
    	$housecat = array (4);
    	wp_set_object_terms( $post_ID, $housecat, 'category');
    	}
    }
    add_action('publish_houses', 'add_housecategory_automatically');

    where houses is the custom post.

    This script works great, but you have to know what ID (in this case 4) the category is. So I tried to extend the script with the following code to auto get the ID of the category.

    function add_housecategory_automatically($post_ID) {
    	$term = term_exists( 'builded', 'category' ); // array is returned if taxonomy is given
    global $wpdb;
    	if(!wp_is_post_revision($post_ID)) {
    	$housecat = array ($term);
    	wp_set_object_terms( $post_ID, $housecat, 'category');
    		}
    	}
    	add_action('publish_houses', 'add_housecategory_automatically');

    now is this not working. The closest I came was to publish automatically in a category called 4. What is the ID of builded.

    I think I am pretty close, can someone tell me what to do different?

Reply

You must log in to post.

About this Topic