Jump to content

I want to create & add multiple category in wordpress programmatically


shaadamin44
 Share

Recommended Posts

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.  

Link to comment
Share on other sites

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.

  • Thanks 1
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.