I think that the best way to set the default category is in the wp-admin
: Settings > Writings > Default post Category However, I think you’re looking for setting more than one category by default.
If you look the documentation of wp_insert_post_data
, there isn’t any key such as post_category
.
Perhaps, you should use publish_post
Runs when a post is published, or if it is edited and its status is “published”. Action function arguments: post ID.
There, you should use wp_set_post_categories
So, the code would be something similar to this:
function set_category_by_default( $post_ID ) {
wp_set_post_categories( $post_ID, array(123, 124) ) ;
}
add_action( 'publish_post', 'set_category_by_default', 5, 1 );
Related Posts:
- Getting a List of Categories for Use in a Plugin?
- How to fetch products with the price in a page on woocommerce using a form or live search with php
- Redirect to another page using contact form 7? [closed]
- Best/Correct way to add an option to a category
- How to add category and subcategory in WordPress custom code?
- How can i listing current category and Featured Category post list?
- How to get Recent Post From Each Category with Thumbnail?
- Add a new category for posts
- How to overwrite the category template in a plugin
- Visual composer custom element error
- How to display the category name in the tab and post inside the tab in WordPress?
- Getting the same post on my related post
- View more posts of a sub-category
- How to insert category name above post title in a featured pages plugin?
- trying to create simple plugin to filter categories from all authors
- Filter EVERY category link with my plugin
- Synch Custom Post Types (and Custom Fields, Cats, etc.) Between WordPress Sites
- How can I add the custom taxonomy categories to the posts and pages?
- Create WordPress category dynamically
- Get plugin directory from a theme
- category_name not working (not showing up in sql query debug)
- What happens when two plugins have the same 3rd party class included into them?
- add_rewrite_rule, plugin activation and plugin deactivation
- Custom admin column disappearing when using Quick Edit [duplicate]
- Create page when plugin is activated
- Update Multiple Post Meta for the Same Post In One call?
- Allowing Custom Capability to Manage Plugin Options
- wp_insert_user – how to send verification email before logging in
- Custom plugin settings: clicking “save changes” does not display success message
- How to un-attach rich text editor from named textarea elements
- Qtranslate displays empty categories with get_categories()
- How can I translate the name of my Plugin for other languages?
- how to not show plugin in admin area
- Getting User email on logout. wp_logout
- How can I filter blog name?
- Logs to check when the plugin was first installed for the first time
- Can a plugin add to header/footer/body content?
- Detect if a plugin was included in a certain page
- Admin page: form with enctype=”multipart/form-data” does not transfer its data
- Show Heirachy of categories in WP Download Monitor
- How do I add a custom sub menu menu under Woo-commerce marketing?
- How to load plugin after page is loaded – pagespeed issues
- How to create custom tables in WordPress using my own plugin?
- How to modify WCMP Rest API response?
- Making a Template for a CPT created by a plugin
- Woocommerce dependent plugin
- External CSS in WordPress Plugin [closed]
- User Session and Stored Cookies not get removed
- get 404 when accessing wp-admin/plugin-install.php
- Custom Pages for Woocommerce Product-Category Pages
- Woocommerce Minimum Price for a Composite Product to add in cart
- add tabs in rdp-plugin/includes/settings.php and get settings saved
- Plugin Development Form Self Submission
- How To Extend A WordPress Plugin Without Losing Your Changes [duplicate]
- Does a blank plugin come with any overhead?
- WPDB: Update table
- Why is my custom post type not being activated on plug-in activation?
- Check filter defined or not?
- How to generate an all in one WordPress New content, plugin and theme update report on a website? [closed]
- Why WP_Screeen doesn’t show all options with admin_body_class
- License validate function
- wp_enqueue_style on template_redirect level?
- Built a second plugin but it overwrote the first one
- Symlink a plugin in local development, works but got Debug error message – Windows 10
- Categories from front-end, checkbox selection doesn’t work
- Custom Post Type template for homepage
- How can i export and import my plugin option WordPress
- Remove all messages, when untrash a post
- Why won’t wp_enqueue_script work within any plugin file?
- Help to Create a Simple Plugin to make a post
- Error using wordpress functions inside a plugin class
- How to resolve warning for `unstableOnSplit` prop on a DOM element in block editor
- Rename a folder via HTML POST request
- add tags to wordpress post using REST API
- path of wp-content directory when we are on some plugin
- how to do Thematic like this website using wordpress?
- Add Custom Field to Post Pages via Plugin
- Timing issue with is_amp_endpoint()
- Taxonomy Category Icon on index page
- Can I add content before post content without using the_content filter
- Why is the WordPress update_option not working in this code?
- what is the best way to create a premium plugin while also protecting your code
- hide load more button if there are no posts left to display
- Input gets deleted/overwritten after changing to different Admin Menu
- Force Network Activated Plugin to Run After Site Level Plugins
- How to set the default options on an existing plugin in a WP MU new user install
- How to use categories in the URL with Advanced Custom Fields?
- Plugin Breaking WordPress Login
- Cannot Search | Featured | Popular | Newest | Favorites in wordpress working
- WP-Snap too slow (caused by WP_Query?)
- Display static pages instead of category, in code
- Checking url from plugin [duplicate]
- Gutenberg: Dynamic Block – Show saved data in the editor
- Is there a way to combine categories and their hierarchy into the admin listing page?
- How to fix ‘Call to undefined function do_settings()’ error?
- wp_handle_upload – specified file failed upload test
- how to make wordpress remember my choice
- Running a function using Crown WordPress on one day a week, for example, Mondays of every week
- How to make a Template page to show the information of different things Shop and Product page?
- wordpress admin-ajax bad request 400