1
votes

I need to assign a default category to my custom post type.

i created this, but do not work:

function add_portfolio_category_automatically($post_ID) {
    global $wpdb;
    if(!has_term(”,’portfolio_category’,$post_ID)){
        $cat = array(11);
        wp_set_object_terms($post_ID, $cat, ‘portfolio_category’);
    }
}
add_action(‘avada_portfolio’, ‘add_portfolio_category_automatically’);

Information:

custom post type taxonomies: avada_portfolio

custom post type taxonomies of category: portfolio_category

I wish all the posts had the category "car" as defaul.

I tried for hours without success.

1
you need a default value for $post_ID ? your question is not clear - Halayem Anis
default value for avada_portfolio, It is a custom post - user5060975

1 Answers

1
votes

You have to pass the "car" category slug or ID as second parameter of wp_set_object_terms.

Try

wp_set_object_terms($post_ID, "car", ‘portfolio_category’);

If "car" is not your term slug, retrieve the term ID first.

$term = get_term_by( 'name', 'car', 'portfolio_category' );
$term_id = (int) $term->term_id;
wp_set_object_terms($post_ID, $term_id, ‘portfolio_category’);