Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

WooCommerce wp_insert_post action firing on every save

My suggestion would be to add in a simple flag that checks whether this is the initial time we’ve set our defaults, something such as below:

function initial_product_data ( $post_id ) {
// lets get out of here if this is not a new product
if ( 'product' != $_POST['post_type'] )
    return;

//Grab our initial meta value, if set will return true else wordpress will return and empty string(falsey)
$initial = get_post_meta($post_id, 'initial', true);

if(!$initial){
    // make sure the post isn't automagically featured
    // WTF WooCommerce??!!
    update_post_meta( $post_id, '_featured', 'no' );

    // automatically set the swatch
    update_post_meta( $post_id, '_swatch_type', 'pickers' );    
    update_post_meta( $post_id, '_swatch_type_options', unserialize('a:1:{s:7:"pa_size";a:4:{s:4:"type";s:12:"term_options";s:6:"layout";s:7:"default";s:4:"size";s:19:"swatches_image_size";s:10:"attributes";a:3:{s:7:"x-large";a:3:{s:4:"type";s:5:"color";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:1:"0";}s:23:"x-large-toplarge-bottom";a:3:{s:4:"type";s:5:"color";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:1:"0";}s:23:"x-small-topsmall-bottom";a:3:{s:4:"type";s:5:"color";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:1:"0";}}}}') );

    //Set our initial check value to true, to prevent the above code running again
    update_post_meta($post_id, 'initial', true);
    }
}
add_action( 'wp_insert_post', 'initial_product_data' );

That should do the trick and only set the default upon the initial creation of the product.

Related Posts:

  1. Why is there both a save_post and wp_insert_post action?
  2. When and Where to use wp_insert_post()
  3. Set default Custom Post Meta Value
  4. How to upload multiple image using wp_insert_post
  5. Re-order Woocommerce single product and echo div around moved & duplicated Woocommerce hooks
  6. Custom Post type forgetting data
  7. Generate payment URL in custom email [closed]
  8. is there an update_post_meta action
  9. How can I get some of a posts meta data whilst it is still inserting?
  10. remove_action or remove_filter with external classes?
  11. add_action reference a class
  12. Why does save_post action fire when creating a new post?
  13. How to update custom fields using the wp_insert_post() function?
  14. Attaching taxonomy data to post with wp_insert_post
  15. Remove parent theme action in child
  16. How to know what priority to use with add_action()?
  17. Check if Post Title exists, Insert post if doesn’t, Add Incremental # to Meta if does
  18. How to do_action and get a return value?
  19. What is “meta_input” parameter in wp_insert_post() used for?
  20. What is the different between an attachment in wp_posts and an attachment in wp_postmeta?
  21. Remove an action from an external Class
  22. Difference between do_action_ref_array() and do_action()
  23. Can my “add_action” function know the name of the hook calling it?
  24. Does the event ‘wp_version_check’ even exist? What is it doing?
  25. WordPress Multisite: Adding actions to only one site
  26. When is admin_init Action ran?
  27. Can an action callback prevent the parent from continuing execution?
  28. remove_action in a theme
  29. How to remove action hook done in a plugin from functions.php in my theme?
  30. add_action ‘manage_posts_custom_column’ in a class [closed]
  31. How do I enqueue a script to run inside the Gutenberg editor?
  32. How can I find out what functions are assigned to actions?
  33. Unable to prevent function using save_post firing twice
  34. Create posts on user registration
  35. Difference between do_action(‘admin_enqueue_scripts’, $hook_suffix) and do_action(“admin_print_styles-$hook_suffix”) syntax
  36. Cron jobs in a class
  37. Add something to beginning of the content
  38. What is the code to get the download link for a product in WooCommerce?
  39. Disable WooCommerce action
  40. advanced custom fields update_field for field type: Taxonomy
  41. Content hooks vs User hooks
  42. How can I remove the WP menu from the admin bar?
  43. Why, Where, and When to use reference pointers in filters/hooks?
  44. Restrict post edit/delete based on user ID and custom field
  45. add_action customize_register not working
  46. Passing arguments into ‘init’ function
  47. wp_insert_post() getting slower the more posts
  48. Add a Custom Field in Comment Box AFTER text area BUT BEFORE Send button
  49. Adding an action within a function that is being called by add_filter
  50. WooCommerce conditional meta query
  51. How to use conditional add_action for WordPress
  52. Using wp_insert_post and post_update_meta but need to fire save_post afterward
  53. WP Schedule Event – Every Day When First Visitor Comes
  54. enqueuing React script and hooking its target div fails to load script
  55. Change meta tags programatically
  56. wp_logout action not working
  57. How to remove an action that is added inside a class
  58. Having an add_action( ‘user_new_form’,) [closed]
  59. Using auth_redirect returns cannot modify header information
  60. add action which returns modified value
  61. What would cause the ‘wp’ action to fire twice per page (but only once per post) in Firefox only?
  62. Problem:Save Several Duplicate posts in The Database and then Error nesting level of ‘100’ reached
  63. How to remove an action added by a child theme of Genesis
  64. I don’t understand why I need a lower priority to remove an action with a higher priority to make it work
  65. Use wc_enqueue_js only on specific pages – nested add_action
  66. How to Remove a Filter from the Admin List Table?
  67. WordPress admin WP_table_list show incorrectly
  68. Query for posts from any post type but only add instock products
  69. wp_get_attachment_metadata returns false with add_action() ‘add_attachment’ hook
  70. How to add a checkbox inside the “Publish post” widget?
  71. Remove action on product archive page [closed]
  72. Manually add admin bar
  73. Is there a way to expose additional fields to the Bulk Action > Edit functionality?
  74. What’s the usage of action do_meta_boxes?
  75. How can I invoke an action after a CiviCRM triggering event?
  76. Custom bulk actions in WP 3.7.1
  77. Remove action from a plugin class
  78. What hook should be used to programmatically create a post only when master post is updated?
  79. do_action pass array argument as reference not copy [duplicate]
  80. How to remove_action inside class [duplicate]
  81. how can I use add_action with external class which the function contain 2 argument?
  82. remove_action not working with procedural function
  83. Why Should We Use wp_clear_scheduled_hook and What it Does?
  84. Woocommerce Change placement with wrapper
  85. Modify query in pre_get_posts action is messing up my nav menu
  86. Disable email field on WooCommerce customer account details
  87. Exclude trash from save_post
  88. How to check if an action is already fired?
  89. add_action and wp_head not display content in head section
  90. Scheduled event won’t fire
  91. Get updated post meta on save_post action?
  92. Which action fire in front-end single post only
  93. When should I call add_settings_section and add_settings_field?
  94. update_post_meta() whenever custom post type is updated
  95. use add_action(‘wp_head’) in a widget
  96. Inserting Hundreds of Thousands of Posts at Once
  97. How to prevent action in ajax request, when in admin side?
  98. Unable to programmatically remove product from WooCommerce cart
  99. Explanation for remove_filter used in the below code [closed]
  100. Unable to show error message using wp_login action
Categories actions Tags actions, post-meta, woocommerce-offtopic, wp-insert-post
Which hook should I use for this scenario regarding the registration process and account/profile update?
Pass jquery var to a function in functions.php

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress