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

wp_insert_post inside save_post adds wrong metadata to inserted post

The reason for that is the save_post loop. When you call wp_insert_post, it triggers save_post and thus inserts the student meta.

what you can do is, to check if the post type is correct while inserting the meta.

like:

add_action( 'save_post', 'save_student_meta', 10, 2 );

function save_student_meta( $post_id, $post ) {
    if ( 'student' !== $post->post_type ) {
        return;
    }
    // Code gathering data form $_POST 

Related Posts:

  1. Set default Custom Post Meta Value
  2. Check if Post Title exists, Insert post if doesn’t, Add Incremental # to Meta if does
  3. Execute action after post is saved with all related post_meta records (data)
  4. advanced custom fields update_field for field type: Taxonomy
  5. Add post meta based on another post meta value before publish post
  6. Get updated post meta on save_post action?
  7. Correct processing of `$_POST`, following WordPress Coding Standards
  8. Delete post meta conditionally after save post
  9. Compare old meta with new post meta
  10. Save post meta foreach loop
  11. troubles with get_post_meta (and saving it)
  12. Insert Custom Field Value
  13. Meta keys won’t add using wp_insert_post
  14. Get post meta after insert post
  15. Update post_meta post_name with the post ID from wp_insert_post after user register
  16. post_title in save_post action
  17. php wp_insert data on front using a form
  18. Auto save title as custom meta field value
  19. Save frontend submission form field as custom field
  20. get_post_meta() returns nothing in save_post, publish_post, wp_after_insert_post
  21. if condition from post_meta not working in save_post
  22. Getting author’s nickname as meta value
  23. How to update custom fields using the wp_insert_post() function?
  24. post formats – how to switch meta boxes when changing format?
  25. How get post id from meta value
  26. Safe to delete blank postmeta?
  27. get_post_meta returning empty string when data shows in the database
  28. publish_post action hook doesn’t give post_meta_data
  29. How to access the post meta of a post that has just been published?
  30. Should I sanitize custom post meta if it is going to be escaped later?
  31. Query between two meta values?
  32. Better post meta efficiency?
  33. List posts under meta_value heading
  34. loop through all meta keys with get_post_meta
  35. update_post_meta() whenever custom post type is updated
  36. post_status => publish not working
  37. Saving html into postmeta without stripping tags – safe?
  38. How to show Published date and/or Modified date
  39. wp_update_user isn’t instantly?
  40. How to use update_post_meta inside wp_trash_post
  41. “update_post_meta” not working in “wp_insert_post_data” hook
  42. update_post_meta, xml parser
  43. Pre insert data when adding new custom post fire a json error
  44. How to create a meta_query to get all posts with a specific meta data?
  45. Attaching taxonomy data to taxonomy with wp_insert_post
  46. hide posts with specific meta data from admin page
  47. Post MetaTable Overload
  48. Custom RSS Feeds & Post Meta Data
  49. Editing does not change post_name
  50. Compare string with post id in wpdb and do stuff when match is found
  51. $wpdb class updating meta_value using Ajax [closed]
  52. How to selected which tags to print, instead of printing the whole tag list?
  53. query posts and split meta information into separate div’s
  54. add unique string as custom-field to every post
  55. How do I insert a new meta key / value pair, but only if another meta key is present?
  56. “Cannot use import statement outside a module” JS error while adding a custom meta block?
  57. Gutenberg featured image checkbox – checkbox not correctly set on editing page reload
  58. How to properly use oneOf and anyOf in Rest Schema?
  59. Get the author meta adds now
  60. wordpress is_front_page() issue
  61. show ad after # paragraphs
  62. If ACF meta_key has meta_value
  63. Can wp_insert_post_data filter be used to save custom field data?
  64. How we can get “get_post_meta” of specific user who added it
  65. Repeated nav bar queries failing to be cached
  66. Randomizing Post Links Outside of Loop – No Author or Date
  67. help to decipher wp metadata
  68. SQL query – get a featured image’s alt / alternative text
  69. I can not display meta value in extras.php and template-tags.php
  70. update_post_meta() updating nested array in Multidimensional array with empty sub-array
  71. How do I list taxonomies that have upcoming events in WP? Is there a way to do this without having to query posts first?
  72. Sorting my posts on homepage my specific value in post_meta table
  73. Strict comparisons problem when using boolean post meta
  74. Why is the actual number of thumbnail images not matching what is store in an attachments metadata?
  75. Check if any meta on the post has value then display content
  76. Combine meta query and give a specific meta query a higher priority
  77. Get table parameter and save in meta value
  78. How can i show post views using specified post ID?
  79. How to add/update post meta to use in query?
  80. gettext localization is not working
  81. Rename image filename using ‘media_handle_upload’
  82. Update Post metafield of specific categories
  83. Custom post type meta fields missing on save_post_type
  84. Using Self Hosted Video URL With Custom Fields
  85. Multiple postmeta values to the same post_id/meta_key combination?
  86. get_post_meta not work in php foreach
  87. Why are my custom metaboxes not updating the post meta?
  88. Problem serializing single quote and double quote into post meta
  89. Storing post_meta fields in array
  90. Performace on 1 million plus meta fields vs 1 field with 1 million multi array
  91. Fetching array of postmeta with $wpdb and in_array conditional
  92. Piklist File Upload
  93. how to echo/display the custom field value in specific custom field name?
  94. update_post_meta() not working in bulk option
  95. How to update the ‘modified_time’ of a post ONLY when content is changed?
  96. Multiple If else statements on Search Result Page
  97. get_children filter with postmeta
  98. wordpress query making site very slow
  99. I read the postmeta and update it without changing it, then move on to the next record
  100. How to append $metadata[‘sizes’] to metadata when using ‘wp_generate_attachment_metadata?
Categories post-meta Tags post-meta, save-post, wp-insert-post
How to find php variable of wordpress theme settings
How to display dropdown filters for posts

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