简体   繁体   中英

Using wp_set_object_terms to add custom taxonomy terms

I'm trying to add two strings as custom taxonomy terms using the following:

$breadcrumbgrandparent = strip_tags($html->find('div[class=breadcrumb-panel] ul li', 1));
$breadcrumbparent = strip_tags($html->find('div[class=breadcrumb-panel] ul li', 2));
$tags = $breadcrumbgrandparent . ", " . $breadcrumbparent;

// vars
$my_post = array(
  'post_title'  => $strippedtitle, //site
  'post_type'       => 'product',
  'post_status' => 'publish',
);

// insert the post into the database
$post_id = wp_insert_post( $my_post );

$taxonomy = 'categories';
wp_set_object_terms($post_id, $tags, $taxonomy);

At the moment it's adding the strings as one term called "Term 1, Term 2" rather than two separate terms called "Term 1" and "Term 2".

Where am I going wrong?

The problem is in $tags value. It should be an array, but you are using imploded string.

Try this:

wp_set_object_terms($post_id, explode(",",$tags), $taxonomy);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM