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
- Is there a maximum length to a WordPress Page?
- Skewed / slanted button within elementor [closed]
- WP.org acceptable iframe alternative
- I changed .live() to .on() but change is not reflected on the server
- How to find out whether a plugin is used in pages or posts?
- WooCommerce – set session with new cart item meta when updating cart item quantity [closed]
- Trying to add a page template for my custom post type from a plugin, but it will not display in the template dropdown unless file is in theme
- flush_rewrite_rules() not working with update_option_{$option}
- How to get the particular product quantity in orders in Woocommerce [closed]
- plugin dev – minimum base css specificity?
- Using WordPress to build membership Page
- How to generate an all in one WordPress New content, plugin and theme update report on a website? [closed]
- Tutor LMS problem with Paid Memberships Pro [closed]
- Is there a plugin for WordPress for creating ‘Accounts’ where all users who belong to that Account can only see Account data? [closed]
- A server-side hook failed when committing plugin code to SVN
- Custom shortcode for displaying user based on a role parameter
- Help with comments.php
- How to execute plugin and theme updates from a web hook / endpoint?
- WordPress Plugin Insert Html Code with Shortcode
- Content-Security-Policy Headers are there and showing the correct settings, but still getting a refused connection
- Set WordPress Transient Expiration via Variable Value
- Vulnerability Concern From the Plugin or From Not Updating the Plugin?
- PHP Warning: Declaration of aec_contributor_list::form() should be compatible with WP_Widget::form($instance)
- How to remove index meta tag
- don’t call ajax if not plugin page
- How to use the pre_option filter before a plugin loads?
- How I can change the required capability for an admin menu without editing the plugin file?
- Why WP_Screeen doesn’t show all options with admin_body_class
- I changed font of wordpress dashboard but it is slow!
- wp_dequeue_script for a Plugin
- Location dependent rendering of locations? [closed]
- Creating a widget and missing jQuery
- SMTP Error: Could not authenticate [closed]
- How to add support for caching plugins for my own plugin?
- My custom php file keeps 404’ing in WordPress when I call it. What am I missing?
- Use $variable from file1.php in file2.php (different file paths)
- How to import WordPress site to new WordPress site?
- How can I automatically download all images from all imported posts, place them on my new host, and replace all the links
- Need to replace Currency Shortforms
- Looking for a free quiz plugin which saves the candidates answers for review [closed]
- wp_enqueue_scripts
- Why Does The `auto_update_plugin` Hook Disable Management?
- Return multiple values in a shortcode attribute
- Allowing .rfa Files In Media Upload [duplicate]
- Sanitizing file & directory form input
- What’s my optimal choice for remote access to my localhost sites?
- Some WordPress Posts are automatically becoming comments on other Posts
- List category posts displaying limited posts
- Get Every Key & Value from Array then Display All in New Line
- What are admin hooks
- disabling ftp on wordpress
- Woocommerce Coupon Menu and Marketing Menu missing After “Coupons can now be managed from Marketing > Coupons.” [closed]
- Is there a way to add custom endpoint to specific page only
- “uagb/section and uagb/columns block”
- IF condition based on wp_remote_get output
- shortcode not return any thing in Gutnburg
- Fatal error on some pages after cancelling WPSTAGING cloning process
- Always get creating a new table with foreign key
- Progmatically adding menu links to the default (Top) or Footer menu
- Add text to variable
- Where is my google maps on my page? [closed]
- standard custom page template hierarchy for plugins
- get specific value of a array | PHP
- How do I add $_SESSION[”] to my wordpress page?
- How to get specific string/value from an array? php [closed]
- coding a WordPress AJAX Form using PHP to check if User is Logged Out and Show error
- How do i show different group of images below a button when the button is clicked on the same page in wordpress? [closed]