Try this:
function custom_set_term_to_post(){
$args = array(
'post_type' => 'post',
);
$all_posts = get_posts();
foreach ($all_posts as $key => $post) {
$tag_name="post_tag"; // required tag name
$term_list = wp_get_post_terms($post->ID, $tag_name, array("fields" => "ids"));
if (!empty($term_list)) {
// there is already tags; need not do anything
continue;
}
$tag = array( 23 ); // required tag ID to assign
wp_set_post_terms( $post->ID, $tag, $tag_name );
}
}
This function scans all posts of post type post
and check if it has any tag or not. If no any tag is assigned, then required tag is assigned. Here tag name is post_tag
. And required tag item is of id 23
. Replace it with your value.
Make sure this function only run once. Just for current purpose. Dont leave it in the theme. 🙂
Related Posts:
- How do I get the top-level terms in a custom taxonomy?
- Get only the first term by taxonomy
- Is it possible to get a Previous/Next Taxonomy term archive URL?
- Which template name to use for taxonomies but not their terms?
- Count number of post in Taxonomy?
- How to include parent terms in hierarchical taxonomy URLs?
- Manipulating cookie on specific taxonomy archive page
- How to edit/remove a term that’s in multiple taxonomies?
- Fix : Custom Taxonomy Duplicating in each call
- Create Admin Only Taxonomies?
- Category page and edit post page with this category very slow
- Text and image in taxonomy page
- Custom taxonomies
- Custom Taxonomy tree view not showing correctly in backend
- Add parent/child taxonomy to custom post type url
- How to add custom field to custom taxonomy in 4.4
- Extending wordpress search to include excerpts and taxonomies?
- Adding ‘active’ class to custom taxonomies using walker in functions.php
- How to output content based on same custom taxonomy?
- How to create custom taxonomy URLs without taxonomy name?
- How to add taxonomy to Users menu in admin?
- Category page for custom-taxonomy shows 404 when using custom permalink structure
- How to insert an array of terms using wp_insert_term?
- Create Advanced search with taxonomies (not filter list)
- Show nav link highlighted
- display taxonomy slug from term ID
- How can i retrieve all custom registered taxonomy?
- How to Put Custom Taxanomy Meta Box in Main Column
- Hook to change Custom Tag Taxonomy Links?
- Redirect all child taxonomy to its parent
- How to add taxonomy to user?
- How to use get_the_terms() to display multiple terms?
- Woocommerce product search result layout page
- Sorting Tags by Slug Value
- List posts of term on term’s admin edit page
- Taxonomy Order by Name
- Change the permalink URL to include a taxonomy term
- How to limit the number of users who can be added to a user custom taxonomy?
- Get Custom Taxonomy Title by Spesific Sub Category via fputcsv
- Taxonomy Pagination Rewrite
- taxonomy tax-name archive
- Set Custom Taxonomy to Entire Site Programatically
- Hiding taxonomies with no children WP_Query & tax_query
- Custom taxonomy for Media Library
- Same term from custom taxonomy in two different custom post types?
- How to fetch the data from Advanced Custom post when we search specific keyword or field name related to the post?
- 2 custom post-type and 1 taxonomy
- Order get_terms() by hierarchy
- Proper way to update the slug of a taxonomy using register_taxonomy? [duplicate]
- How to customize taxonomy archive template files for subterms?
- Query posts using custom taxonomy and selected terms
- Foreach loop duplicating a taxonomy thumbnail image
- get_the_tags Conditional display of a custom taxonomy
- Output all terms in a custom taxonomy and add a “active” class only to the ones attached to the current post
- one post per term taxonomy
- How can I change the output display of my pagination?
- Hide custom Woocommerce taxonomies when empty
- Custom permalink accept any params
- Terms change id after importing
- Hide custom taxonomy from easy admin
- forward/redirect taxonomy archive of term to a page with same name
- how to use tax_query to apply both terms or one if one is empty
- Get current page term title to use in WP_Query
- Show related posts on single.php, grouped by taxonomy terms, with Advanced Custom Field post object selected
- making an index from custom taxonomies and tags of posts (not hierarchical taxonomy)
- Displaying my custom taxonomies in a using the_term_list()
- Using Custom Taxonomies to organise products – struggling to call a correct list
- Use wp_list_categories to list parent categories from actual term
- is there a way of getting term children that only shows those from two taxonomies
- Get Posts Related to Category By category id or slug
- How to add a dynamic dropdown derived from custom taxonomy
- Order get_terms by count using a custom taxonomy hierarchy
- How to add chained dropdown filter to a wordpress website
- How to create a non removable taxonomy term?
- How to show category list in WordPress
- custom taxonomy not showing up after adding capabilities upon updating to WP 5.1.1
- transfer two existing taxonomy terms into one custom metabox
- How to add all existing Custom Taxonomy to Admin Menu Creations Interface?
- Change the Page Title of the Archive Page for Portfolio Tags
- Is it possible to sort the post based on a custom field?
- Catchable fatal error: Object of class WP_Error
- Permalink structure not working with Custom Taxonomy (URL like ./taxonomy/category/postname)
- Creating a Taxonomy Page [duplicate]
- WordPress shortcode Issue!
- Switching from catalog to product category/subcategory
- Extra fields on categories that is available via the WP-API?
- Tax Query not working
- Taxonomy archive page have no post
- Display list of custom taxonomies from a custom post type ordered by their parent
- What’s the proper WordPress URL for the template of my custom taxonomy?
- WordPress show posts from children terms of a taxonomy term
- how to import custom taxonomy (& terms)
- How to create a page that lists custom taxonomies with links?
- Getting taxonomies specific to categories
- How to add/enqueue Custom CSS for a custom taxonomy page?
- Search and filter with custom field in taxonomy
- is_tax not working for custom taxonomy
- Custom taxonomy with custom meta value is not sorting correctly (query returns the same value for orderby regardless of sort column click)
- wp_add_object_terms cant recognize a post ID
- Foreach for get_the_terms for hierarchical taxonomy don’t repeat Top Level Terms if contains multiple Second Level Terms