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 create a dynamically created page on wordpress plugin?
- How to extract data from ‘edit my profile’ page in WordPress?
- Fatal error in form display plugin
- Invalid plugin installation directory, Google Analytics [closed]
- Is it possible for a plugin to force reading and writing of uploaded images to a database instead of to a filesystem?
- Image change on hover
- Need to show featured service boxes on my company website
- $wpdb update query in plugin only updating one column
- Why do plugins often ask to add in to templates?
- Is it fair to use MIT licensed code in WordPress plugins? [closed]
- Plugin retrieving results even after uninstallation
- Editing a text file from plugin menu
- Does using `add_action( ‘init’…` cause performance issues?
- Setting Up PHP Workflow in WordPress
- How to add an element right after the article using jQuery?
- How to reset wordpress but not plugins and settings
- AJAX Load more start with x posts
- php code crashes site – possible syntax error
- White screen after plugins upgrade
- How to add a dvi tag to a shortcode then change a generated text using jQuery
- do_action Nested List
- Deactivate JS Script in Plugin Shortcode
- How to automatically add edit link on frontpage post of any wordpress theme?
- Child Themes and Updating Parent Theme
- How to write “alt” tag in image for wordpress code?
- plugin active error
- How to get a list of bundled products using wp_query in woocommerce
- WordPress reliability
- Allow only Admin role to access plugin settings/options page
- Is it safe to use admin-ajax.php in the frontend?
- Include a php file from another plugin
- Getting a Page via its post-name using WP REST API v2 and Postman
- How to get all posts belongs to a user between a certain date
- Add estimated value for a post according to the number of words
- Passing stored variables to add_filter
- Does heavy theme and plugins affect server’s response time? [closed]
- How can I delete a wordpress widget instance?
- order posts by `meta_key`, Does not display posts that does not have `meta_key`
- Repeated Output in Widget
- How do I make a secure API call from a shortcode when a button is clicked?
- A question about register_activation hook
- Adding Static Image to WooCommerce Content-Single-Product template
- plugin shortcode not working on ajax request call
- Displayed content disappears when adding attributes to shortcode
- Modify page title and subtitle with a plugin
- One sub site is slow while all others load fine, same theme & plugins [closed]
- Issues with a WP plugin menu system
- How to protect WordPress from security scanner [closed]
- Add back to post link on blank comment post error page
- override filter in a plugin
- Check filter defined or not?
- How to track clicks
- How to load different homepage on Mobile.?
- How do I pass custom shortcode-extracted variables (taxonomy) into a query function for WordPress RoyalSlider?