After getting year, you may check if category exists using
category_exists($cat_name);
if not found then create category using
wp_create_category( $cat_name, $parent );
after that you may create post using
$my_post = array(
'post_title' => wp_strip_all_tags( $_POST['post_title'] ),
'post_content' => $_POST['post_content'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 )
);
// Insert the post into the database
wp_insert_post( $my_post );
You may also add category to a existing post using
wp_set_post_categories($post_id,$post_categories)
more information:
https://developer.wordpress.org/reference/functions/wp_insert_post/
https://codex.wordpress.org/Function_Reference/wp_create_category
https://developer.wordpress.org/reference/functions/category_exists/
Related Posts:
- Hook when category is added to post
- get_field not displaying my custom field value
- Send an email when custom post type category is changed
- what is the action hook code to supporting product category condition in single product page of woocommerce?
- Advanced Custom Fields: Using category as a rule, before publishing new post?
- Remove word “Category” from WooCommerce product page [closed]
- advanced custom field Show field in special categry page [closed]
- Replace category titles
- functions.php conditional tag – only show in single.php?
- Sorting posts by season
- Set the default category of an attachment
- How to loop only categories without posts (+ show category featured image with acf) [closed]
- ACF: Display category name using taxonomy field
- How to hide category name
- ACF add custom fields to categories and display
- How to query categories with Advanced Custom Fields?
- Categories widget show empty?
- How to display a value inside a post with a specified category from a category custom field? [closed]
- How to access deleted term inside delete_product_cat action
- Query (built in) Categories and Advanced Custom Fields?
- ACF no print data [closed]
- How can i add service category here? [duplicate]
- How to hide a category or tag everywhere EXCEPT for use with get_posts
- How to set acf color field as background color to product category
- How can I add different custom fields for each category in the same post?
- Correct Hook/Filter to amend category choices on post edit page
- How to create a category loop on my blog (ACF Plugin)
- How to delete WooCommerce categories in bulk that only have one product
- Ajax Category add doesn’t update the list table custom column
- In ACF, how to show custom category selection tab upon selecting a main category
- How to hook into creating a category?
- get categories the post was in and just been removed from
- ACF plugin categories bug in WordPress 3.9
- My Query is getting the wrong data
- How to use acf field value to order category?
- How to display the category featured images [closed]
- Hook action when create new category [duplicate]
- How to link ACF relationship field to the new Query block in Full Site Editing
- Replace category titles inside RSS feed
- How to create an automatic MultiColoumn MegaMenu with Categories WordPress
- Is There a Difference Between Taxonomies and Categories?
- List all subcategories from category
- How To Find Out WordPress Category Table in MYSQL?
- Add custom field to Category
- Get Category ID inside Category template
- Only one category per post
- WordPress Multisite – global categories
- Get the children of the parent category
- Get posts from sites in Multisite?
- add_action hook for completely new post?
- WP REST API: filter by category 1 AND category 2
- How To Get Parent Category Slug of Current Post
- Display All Products by Category with WooCommerce
- What is the difference between a “tag” and a “category”?
- Is there an easy way to make a meta box have the tabs like the Categories meta box has?
- How to check if I’m on the last page of posts?
- Display posts from the same category using next/previous post link
- How would I get a taxonomy/category list inside a Gutenberg block?
- Can I set a default featured image for a category?
- Check if a post is in any child category of a parent category
- How to export and import taxonomies (category, tag and/or custom taxonomy) and their terms
- Force category choice before creating new post?
- Gutenberg editor add a custom category as wrapper for custom blocks
- WooCommerce: List All Categories
- How to get category and archive title?
- Get category ID from category slug (not working)
- How do I remove “Uncategorized” from posts with more than 1 category?
- Get rid of WordPress category, tag and author archives?
- Individual rss feed entry length for categories?
- How can I create a category landing page followed by pages of posts?
- Remove parent category from permalink? Basically only have the child category?
- Custom permalink structure leads to be 404 on pagination
- Retrieve Product category ‘NAME’ by product category ID – WooCommerce?
- Posts URL structure like site.com/category/the-post-title
- Can’t custom taxonomies have same terms (slugs) as categories?
- How to get permalinks with category base working with sub-categories
- How to remove the term “category” from category pagination?
- Using wp_category_checklist in a widget
- WP 3.9 TinyMCE no longer loads on category description editor
- On the category page, get the category object
- Get only the top level categories using get_categories() without foreach loop
- Get all categories and posts in those categories
- How to add featured image for category (without a plugin)?
- How to create a custom sort for WordPress categories
- How to display non-hierarchical taxonomy as terms with checkboxes?
- Category archive by year with permalink support /category/YYYY
- Best Way to Redirect Category to Page and Hide Category
- get_terms vs. get_categories: does it matter?
- What is a good robots.txt?
- Allow users to create their own feed from selected categories?
- get_categories for custom post type with a specific custom taxonomy attached
- Add a URL prefix to permalinks of one category of posts only
- How do you add categories to custom post types in WordPress?
- Create a category list page
- Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
- Woocommerce get category image full size
- exclude category from get_posts?
- How To Modify The Loop in archives.php To Have 11 Posts Per Page and CSS Styling
- How can I hide a category from Contributors in the edit/add new post screen?
- Order get_terms() By Custom Field