I was able to solve this by checking if the term exists, and then using tax_input
in wp_insert_post()
// Check if the category exists
$category_term = term_exists(trim($category), 'formula_types', 0);
// Create category if it doesn't exist
if (!$category_term) {
$category_term = wp_insert_term(trim($category), 'formula_types', array('parent' => 0));
}
array_push($formattedCategories, (int)$category_term['term_taxonomy_id']);
$post = array(
'post_title' => $results['drug_name'],
'post_type' => 'formula',
'post_status' => 'publish',
'tax_input' => array(
'formula_types' =>
$formattedCategories,
)
);
Related Posts:
- Creating 20,000 Posts or Pages using a .csv file?
- Woocommerce – Hide “add to cart” on free products
- Add image to post from external URL
- How to use wordpress $polylang->model->set_post_language in custom php code?
- code is working properly in Core PHP but writing coding in WordPress
- CSV syntax for WP All Import upload without ACF
- Creating a CSV with PHP inside the plugin directory
- Create WordPress posts from JSON array using plugin in admin
- Insert a Woocomerce product in specific product category
- Somehow adding to “Insert Into URL” posts are contradicting each other
- Can export gallery but can’t import it Wp all import
- Is it possible to pin a post in second position from top
- wp_insert_post not inserting post from XML
- best way to upload a large file to wordpress using wp all import
- wp_insert_post with data from graph api inside a plugin
- Export to csv adding html to file
- On save_post need to wp_insert_post and save partent post id to child post and child post id to parent post
- multiple string replace of post’s content for large data
- Products aren’t detected if CSV importer are used
- How can I automatically send an email with an excel file containing submissions for a form rather than an email per submission?
- Choose default template for a page with code
- Is it possible to get a shortcode generated by a plugin, then, using a function, create a new post using that shortcode?
- Issue: CSV file upload works only when “View page source” is done
- How to add featured image on front end form?
- wp_insert_post() post_author and return post_id are not working
- Create csv file in plugin
- Action hook “wp_insert_post” works but not for last imported post
- wp_insert_post hook not being called with wp_publish_post or wp_insert_post?
- CSV file header
- wp_insert_post() get Fatal Error from Plugin
- Is it possible to add permalink when using wp_insert_post?
- Suitable hook when creating, updating and deleting posts programmatically
- How to set an exact search box in tablepress plug-in
- How can I prevent a plugin from updating unless it’s minimum PHP version is met?
- wp_enqueue_scripts not working in custom theme
- What characters are allowed as a shortcode tag and how should they be sanitized?
- Additional, optional fee on Woocommerce Checkout [closed]
- Why does including a file in theme’s functions.php not work?
- OEmbed from Pinterest
- disable active plugins for specific theme
- Something is generating spam pages on my site
- Flickr plugin that will display photos from multiple group pools? [closed]
- How to change ID of an attachment in wp_posts and its related tables correctly?
- Making menu link open in new tab?
- Single Must Use Plugins Directory for local development
- ReOrder Post Within Categories plugin with featured image
- How to initialize something in unit test before the init hook being called?
- Confused about shortcode and settings values
- Running jquery script after shortcode [closed]
- add_filter comment_edit_redirect not working
- Global matrix for shortcodes/content for every page
- Advice for a voting / multiple rating plugin for post types other than GD Star Rating [closed]
- How to write a plugin to add users to a mail list
- Force Plugin Updates: “Update Failed: Plugin update failed.” after update one plugin
- Two text domains in one plugin
- Plugin or method to delete uploads not in media library?
- Add woocommerce variation images in gallery?
- Add a CSS class based on categroy id to archive pages
- Add button gutenberg help
- Comment section not showing correctly
- add input in custom javascript from WordPress pages/posts
- WP Plugins – Use includes to display page content
- Troubleshooting Plugins.php HTTP install path vs. a HTTPS for a single Plug-In
- Save values from a pre_post_update
- WordPress as a data-store?
- How do I use add_action on custom widget?
- plugin active error
- (WordPress 5.8.3) Function.php or a plugin for better performance? [duplicate]
- Control page content visibility based on URL parameter
- WooCommerce Shopping Cart Not Initially Showing Products [closed]
- Elementor Custom Control: How to get input value?
- Print posts category or tag as a formatted journal
- Creating a search form and displaying results
- should i use GLOB_ONLYDIR to hook add-ons for wordpress plugin
- How to downgrade plugin?
- Build a must-use plugin that tracks when other plugins are activated or deactivated
- Using sessions or an alternative in a plugin
- redirect to homepage once action is completed
- taxonomy_template filter not working on pagination [duplicate]
- WordPress Contact 7 Form – Remove WordPress from the From Sections
- How connect my plugin translation to my page
- Dynamic input fields in WordPress Plugin
- Custom role not listed
- How to fix blog article view and share #’s?
- How to Prevent User From Submitting Property Or View His Posted Properties Before Login in wordpress?
- WordPress SSO SAML
- Attachments plugin cannot click media library button [closed]
- Is there any hook to change media html in post? [duplicate]
- plugins_loaded action is not working properly
- Creating a user ‘add custom field’ section
- WordPress Settings – Custom Button actions
- After installing plugins, changes don’t appear
- Links on FAQ page aren’t working
- How can I make the Twitter Tools plugin hyperlink every link?
- How to add a plugin in WordPress theme directory?
- wp insert post not working
- WordPress shortcode plugin not working
- Multiple Post Thumbnails Not Returning URL for Background Image
- Is there any WordPress Plugin that provides video editing from front end side? [closed]
- Disappearing plugins