Well, what is the expected behavior? You use wp_set_object_terms
and pass just a single term.
If you want to make sure the specified term is assigned to the post, while being able to add as many other terms as you like, try it like this:
function add_bookcategory_automatically( $post_id ) {
if ( ! wp_is_post_revision( $post_id ) ) {
$bookcat = 4;
wp_add_object_terms( $post_id, $bookcat, 'category');
}
}
add_action( 'publish_post', 'add_bookcategory_automatically' );
See wp_add_object_terms
.
Related Posts:
- List all subcategories from category
- Get the children of the parent category
- How to remove the term “category” from category pagination?
- Force WordPress to Show Pages Instead of Category
- How do I get the category URL from get_the_category?
- Display all posts in category, with specific tag posts at top
- How to Set an Individual Homepage for Each User?
- Is there a way to remove categories in bulk?
- Is it possible to put next and previous category links?
- Custom category template pagination problem
- Deleting All tags except categories Wp database
- Getting category before saving post
- How do I change the sql query for wp_get_archive()
- Woocommerce – Get product category 2nd level category
- Hide post meta data on a specific category
- New categories are now going to 404s
- How to put enctype=”multipart/form-data” in categories form?
- How can I get an tag to wrap each ancestor that gets outputted in this condition?
- How to prevent empty category to list content of sub categories?
- “Reversable” and “Re-useable” Subcategories (or other taxonomic structure)
- What is the advantage of using header-catname.php over is_category(‘catname’);?
- Insert Into Sub-child Menu
- Change the category of all posts in it
- Search Posts / Pages with multiple options?
- Category location styling
- Get Tags specific to Category using WooCommerce plugin
- Giving wp_list_categories the class of the category
- WordPress category page redirects in .htaccess not working
- Category archive rewrite rule to include category/subcategory?
- How can I automatically display a visual archive of subcategory posts [and thumbnails] separate from Parent category?
- The default code for “posts_nav_link” on category.php isn’t working
- How to organize custom categories for filtering and selecting
- Woocommerce custom category pages
- Custom sub taxonomy order by
- Hide uncategorized when no category set
- category doesn’t showing up in admin
- Refine/Filter the Search Results by Category
- Have multiple category queries from the URL been fixed yet?
- Why is it so hard to show the URL of the current category and how to do it?
- How to make subcategory for custom post and and display?
- Create menu locations for each category in wordpress
- How to get Next/previous_post_link to go through articles in top parent category
- Conditional loop for category
- Fix Warnings that shouldn’t be necessary to fix
- How to make the ‘delete’ button inactive on some categories?
- Notice: Trying to get property of non-object in : custom fields category
- Array: What is this value based on?
- Thumbnail for categories / taxonomies plugin?
- Pagination for query_posts();
- echo get_post_meta of all post in a category to fill up a select field
- WordPress list categories dropdown with parent-child relation and child under it’s parent
- How to Link to Most Recent Custom Post of Same Term
- WP REST API not retrieving all categories
- Category page not showing
- Notice: Undefined variable: category_id
- Change RSS format for specific categories
- I need to exclude from a query a category and a few custom taxonomies
- Displaying a custom field in Category Archives
- List post only under the category, exclude child category content
- Detect category choice for posts with multiple categories
- wp_insert_link not working
- Redirect Problems with Archive Page and GET variables
- Different size video display for category page (smaller) & detail page (larger)
- Adding tinymce to Custom Field boxes on Category edit page
- wp_list_categories : how do I sort by ID when IDs are not sequential
- Showing different posts on category pages
- add current-cat class to single post page
- How to exclude certain categories showing in Gutenberg?
- Problem with pagination block in Gutenberg
- How to redirect to category page if page does not exists?
- Sort categories by custom field in WordPress admin
- Display the articles of a category by role
- Remove “Category:” from Post meta
- Related to genre and category
- Get permalink to latest post in category
- How to load terms of a custom taxonomy of a product in woocommerce cart page
- restrict category to only logged in User
- Category and tag urls return 404 error
- Dynamically creating content on custom URL without getting 404
- Removin /page/2 from pagination
- get parent_category class in loop
- Group Posts by First Letter of Title categories
- Category foreach Paging
- Display all of a post’s categories except the current category
- Alphabetize all but one category
- One category not displaying template updates
- How to display only category of blog without custom code?
- How to create a category or tag available exclusively to the admins and not to the users?
- Printing direct descendants of a category with WP_Query
- wordpress category.php query for featured news item, broken pagination and repeating posts
- Form Value of drop down category + Translation of ‘show option none’
- Echo class depending on Parent category
- Query pages by category
- List all authors by matching custom meta data on a category page
- Page view: Sorted by categories
- Anyway to hide a Category in the Categories section when adding/editing a post in WP Admin?
- How to display posts by author in a separate page, other than author.php?
- One color to category link depending on category ID
- Show subcategories and hide posts or show posts if not exists subcategories
- Images as Categories