shaadamin44 Posted June 29, 2021 Share Posted June 29, 2021 I've a code that can create category if not existed the category & add this to the post. If category already exist then it will select the category. Here is my code snippet example : $mcptitle = 'Lenevo xiaoxin pad pro 2021'; //Geting Brand Name $brands = $mcptitle; $arr = explode(' ',trim($mcptitle)); $brand= $arr[0]; //Notice Brand name Detected //category Slug $catlink=sanitize_title($brand); //Finding category by name & geting id if($term = get_term_by( 'name', $brand, 'category' ) ){ $cat_id= $term->term_id; }else{ //creating category for not exists category $terms= wp_insert_term($brand, // the term 'category', // the taxonomy array( 'slug' => $catlink )); //geting new category id $cat_id= $terms['term_id']; //post start $post = array( 'post_author' => 1, 'post_content' => $content, 'post_status' => "publish", 'post_title' => $title, 'post_type' => "post", ); $post_id = wp_insert_post( $post, $wp_error ); //seting category wp_set_object_terms( $post_id, $cat_id, 'category' ); That works for create/add one category But I want to add multiple categories in it. Category array will be like this: $mcptitle = array("Volvo", "BMW", "Toyota"); Plz Can anyone help me about my thoughts. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/313008-i-want-to-create-add-multiple-category-in-wordpress-programmatically/ Share on other sites More sharing options...
cyberRobot Posted June 30, 2021 Share Posted June 30, 2021 You could use a loop. For example, $mcptitles = array("Volvo", "BMW", "Toyota"); foreach($mcptitles as $mcptitle) { //your existing code here... } Note that I changed the name of variable that holds the array. That way it should hopefully work with your existing code. Also note that I didn't review your code since it sounds like it's working. 1 Quote Link to comment https://forums.phpfreaks.com/topic/313008-i-want-to-create-add-multiple-category-in-wordpress-programmatically/#findComment-1587690 Share on other sites More sharing options...
shaadamin44 Posted July 1, 2021 Author Share Posted July 1, 2021 On 6/30/2021 at 7:31 PM, cyberRobot said: You could use a loop. For example, $mcptitles = array("Volvo", "BMW", "Toyota"); foreach($mcptitles as $mcptitle) { //your existing code here... } Note that I changed the name of variable that holds the array. That way it should hopefully work with your existing code. Also note that I didn't review your code since it sounds like it's working. Sorry for late reply. Thanks for the suggestion. After I follow your example now I'm able to create multiple categories but it's not selecting all categories. It select only one category. Can you plz help me out how can I select all categories? Quote Link to comment https://forums.phpfreaks.com/topic/313008-i-want-to-create-add-multiple-category-in-wordpress-programmatically/#findComment-1587726 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.