Created
June 6, 2016 02:30
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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