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
- Hide wordpress plugins from list
- WordPress plugin from own server
- Adding unit ammount after WooCommerce price. Example $ 24 /m2 and $24 / m
- Moving image attachment from post to another?
- Under which circumstances is the .maintenance file not deleted?
- Autoload via composer in plugin interference
- WordPress plugin form not saving data
- wp_mail attachment not working
- Plugin exceeds memory limit
- Plugin Paths Issue
- Woocommerce plugin for minimum order and add-to-card-step
- Load custom PHP on a custom URL via a plugin
- WordPress Settings Lost After Site Migration
- How to put JQuery/Ajax inside shortcode?
- WordPress Widget Not Saving Data
- options creation for plugins
- Users are being redirected to main site when trying to register on subsite of my Multisite network
- Slider/text combination plugin
- How to disable a widget when dependent plugin is inactive?
- custom uploader in the admin area
- How do I add the same contact form to multiple wordpress sites and capture the response in one place or database?
- How to do the simplest possible frontend ajax call from a plugin?
- New bulk action to resend welcome emails
- I receive taxonomy id
- How do we update a custom file upload field with the Advanced Custom Field plugin?
- How to Include Fields in Query String When Making Request of WordPress.org Plugin API?
- Custom plugin – load enqueue only for this plugin
- Function added to hook “new_to_publish” not executing – custom plugin
- How to display a number of posts based on a Advanced Custom Field (ACF)
- How do I use a custom theme for the WP Maintenance Mode plugin?
- How to get Facebook comment plugin in blog? [closed]
- value not set in contact form 7 plugin of wordpress [closed]
- uncaught TypeError: Cannot read properties of null (reading ‘classList’) custom plugin
- How do I minify a plugins CSS/JS code?
- Elementor Pro display featured image on section -> style -> image using shortcode
- I need plugin or method suggestion
- WordPress – send digital product with custom email
- Litespeed brokes my css
- German Market: How can I only show the imprint in the footer of the mail? [closed]
- Trying to add a new feature in my wordpress website
- How can I implement radio buttons with icons in Contact form 7?
- SEO Site Title appearing in google search despite not being added
- Missing files in enqueue actions causes WordPress to reload
- Adding/ Removing actions for plugins
- Why user profile update creates Additional Capabilities
- Adding list of Indexes in wordpress document
- API calls on plugin activation or init?
- Unfortunately removed myself as plugin committer
- Category archive in menu
- How can I Customize My WordPress Admin Dashboard into Dark Mode?
- Remove on behalf of in wordpress emails?
- Plugin problem. Question is not appearing.
- Embed php code in custom field of a plugin [closed]
- How to make a rest style plugin?