For non-hierarchical terms (such as tags), you can pass either the term name or id. If you pass the id there is only one caveat: You must pass it as an integer, and it must be in an array. This is necessary because any non-array value passed will be converted to a string, which will be interpreted as a term name.
$tag = '5'; // Wrong. This will add the tag with the *name* '5'.
$tag = 5; // Wrong. This will also add the tag with the name '5'.
$tag = array( '5' ); // Wrong. Again, this will be interpreted as a term name rather than an id.
$tag = array( 5 ); // Correct. This will add the tag with the id 5.
wp_set_post_terms( $post_id, $tag, $taxonomy );
This function will only work on the native post type.
Related Posts:
- How can I allow commas in tag names?
- How to customize woocommerce related products? [closed]
- How to get home page ID?
- How to not allow users to create new tags, but allow to them to use existing ones
- Plugin to restrict non-admin user to existing tags
- Creating a WordPress admin page without a menu for a plugin
- Tag selector like stackexchange?
- Remove from Google Tags with less than 2 posts
- Tag link suggestion plugin
- Transient unique names
- How to add tags in Submenu page or Menu page
- how to add text to posts from tags
- WordPress “Categories to tags converter” not working. Pointing to import.php instead
- How to get post ID with hooks publish_post, new_to_publish, etc
- the_tags : can we insert a class
- Form that generates an ID for the customer
- Get Image Having the ID [closed]
- How to transfer data from an additional field in the tags?
- Search by Attachment ID
- WordPress “Add new tag” button is not working
- Get Page URI for QR Code PHP [closed]
- Is there any necessary to use tag plugins like simple tagging in wordpress 3.0+
- how to create hierachial taxonomy and manage under single post type?
- Best approach to make all tags searchable by the wordpress search function
- Give custom page template a custom post ID
- Bookmarking custom template pages which all have the same page ID
- Custom Post Type with Sequence ID
- add tags to wordpress post using REST API
- I want to display “how many times this tag used in WordPress?”
- Get Current Post ID in WP Loop For GiveWP
- Prefix WordPress Taxonomy Tags With Hashtag Symbol Like Twitter
- Exclude Tag Links from and Text
- How to change the product short description for each product with PHP?
- AddThis Plugin adding html comments in P tags
- Find all strings between an enclosing shortcode
- Trending Tags based on post views
- Is there a function to search for a wildcard value when calling a shortcode?
- Get page content by ID (from a plugin)
- Multiselect value in wp_query
- Strip shortcode from excerpt [duplicate]
- WordPress “Categories to tag converter” not working on imported Blogger posts
- Customize Multi-Column Tag Map to fetch specific post-type
- how to add “alt” for all image in wordpress
- Fetching Video From YouTube Automatically [closed]
- Real estate schema markup not showing up on Rich Results test
- How to export post tags from wordpress
- How to add FB like + share AND also other Social Buttons on ALL pages and posts?
- file-gallery plugin line 1573 broken in 4.2 upgrade [closed]
- PHP Error When a plugin is active
- Errors when trying to use a plugin to write to the database
- Completely disabling password reset/recovery
- WordPress REST API Plugin Development – Add Custom Endpoint
- how to prevent all css and javascripts files from loading
- wp_redirect doen’t work
- Hardcoded paths in my plugin
- How to save plugin custom settings page fields
- Dequeue, Unregister, Remove Action – Not Working on Plugin
- HTTP requests start hanging after I try activating all plugins, and only stop when I manually disable the plugins via DB
- Memory Management when developing plug-ins
- How can recognize post’s category?
- Developing plugin, where to place action hooks etc?
- Widget content turns up before title
- Load script only in the admin page
- Using AJAX to run SQL statement and populate dropdown
- How do I fill the Home Description?
- Disable “auto embed” feature when pasting text and links
- How to prevent users to view server files using WP File Manager plugin?
- Alternative Hook to the_content for Changing Background Color
- How to use WordPress header function and footer function and not load word press database
- SASS not showing in customized preloader plugin
- Lock content in WordPress website
- After updating theme & plugin I am getting an error message
- Password Protect wp-content?
- Unable to activate themes on localhost
- How to make cover image in post block expand to entire screen?
- Conditional Fields depending on checkboxes
- Identify if the_post hook is being called from the admin post list
- Translation plugin with default language?
- wordpress ajax pagination object value does not change
- Reorganization of namespaces
- Passing a parameter to filter and action functions
- image upload error wordpress
- OOP Plugin: Where should I place the action hooks in the class?
- How to get the real address from a url (permalink)
- 404 Page when emptying spam or deleting a plugin
- File permissions for wp-minify plugin
- Add country flags
- Content expands beyond its column container [closed]
- fatal erro in one line if statment in wordpress plugin [Solved]
- Add different sizes packaging boxes to orders plugin
- Can’t use updated variables in handle function
- Change email notification language based on the site language in WPForms
- Relationship field only loads items to administrator
- Is it possible to create post in wordpress using postman?
- How does one add numbers to an admin menu?
- Frontend Enqueued Files in the Backend
- Looking for an “animated hero” widget or tech on a WP site
- Remove posts that start with similar words (like a delete duplicate posts plugin)
- _sold_individually value change when add to cart
- Webpack configuration for multiblock plugins behaves unexpectedly