Skip to content

Instantly share code, notes, and snippets.

@ricardobrg
Created June 6, 2016 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricardobrg/7f1293cdc48316879f32912967e35928 to your computer and use it in GitHub Desktop.
Save ricardobrg/7f1293cdc48316879f32912967e35928 to your computer and use it in GitHub Desktop.
How to set multiple taxonomies to a post in WordPress using an array or terms.
//With term IDs
$categories = array('1','2','3','4','5');
$cats_to_add = array_map ('intval', $categorias); //ALWAYS CHECK IF IDs ARE INTEGERS!!
//With term names
//$cats_to_add = ('categoria 1', 'categoria 2', 'categoria 3', 'Categoria 4','categoria Cinco');
if(wp_exist_post_by_title($string['title'])){
echo 'post exists';
} else {
$my_post = array(
'post_title' => $string['title'],
'post_content' => $string['content'].'<br/><br/>'.$string['content2'],
'post_status' => 'draft',
'post_author' => 1,
'post_category' => ''
);
$post_id = wp_insert_post($my_post);
//with post_id we can set object terms
wp_set_object_terms($post_id, $cats_to_add, 'category');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment