• Hi,

    I have a custom post type (‘my_post_type’) with a 3 level hierarchical structure grandparent > parent > child. When I create a new post I currently:
    1) select a eg parent from the Attribute dropdow
    AND
    2) select the category for grandparent ‘G’ > parent ‘P’ > child ‘C’ from the category tick box.

    Each grandparent is always assigned to category ‘G’.
    Each parent is always assigned to category ‘P’.
    Each child is always assigned to category ‘C’.

    Is it possible to automatically select the category depending on the ‘post’ being a grandparent, parent or child?

    I found this snippet which assigns an author but can’t figure out how I could use this to apply to a custom post type post with grandparent, parent, child hierarchy.

    function add_category_automatically($post_ID) {
        global $wpdb;
        $postsWeWants = $wpdb->get_results("SELECT ID, post_author FROM $wpdb->posts where ID = $post_ID");
        foreach ($postsWeWants as $postsWeWant) {
            if(($postsWeWant->post_author != 30) && ($postsWeWant->post_author != 29) && !in_category('bundle')){
                $cat = array(9547,9742);
                wp_set_object_terms($post_ID, $cat, 'category', true);
            }
        }
    }
    add_action('publish_post', 'add_category_automatically');

    Thanks for your help.

Viewing 1 replies (of 1 total)
  • Thread Starter RobertBrisbane

    (@robertbrisbane)

    So I found this here, it assigns the category to the custom post type but the hierarchy for the parent child is still missing.

    function add_g_category_automatically($post_ID) {
    	global $wpdb;
    	if(!has_term('','category',$post_ID)){
    		$cat = array(4);
    		wp_set_object_terms($post_ID, $cat, 'category');
    	}
    }
    add_action('publish_my_post_type', 'add_g_category_automatically');

    any thoughts?

Viewing 1 replies (of 1 total)
  • The topic ‘Auto assign category to Post (grandparent > parent > child)’ is closed to new replies.