Saving data for multiple custom post types
-
I am adding a couple different custom post types to a blog, and am running into an issue when I try to save data for the second CPT (custom post type).
I have the code as follows for the first CPT (not the complete code obviously):
... snip ... add_action('save_post', 'save_team_data'); function save_team_data( ) { global $post; // don't compare and add and edit fields here... // just delete the lot, and re-add delete_post_meta($post->ID, 'field_name'); delete_post_meta($post->ID, 'field_lat'); delete_post_meta($post->ID, 'field_long'); if (isset($_POST['field_name'])) { foreach ($_POST['field_name'] as $key => $null) { if (trim($_POST['field_name'][$key])) { add_post_meta($post->ID, 'field_name', trim($_POST['field_name'][$key])); add_post_meta($post->ID, 'field_lat', $_POST['field_lat'][$key]); add_post_meta($post->ID, 'field_long', $_POST['field_long'][$key]); } } } }
and the code as follows for the second CPT:
... snip ... add_action('save_post', 'save_game_data'); function save_game_data( ) { global $post; update_post_meta($post->ID, 'home_team', $_POST['home_team']); update_post_meta($post->ID, 'away_team', $_POST['away_team']); }
You’ll notice that it’s basically calling the add_action(‘save_post’) function twice with different callbacks. I’m assuming this is where the error is coming from, as it’s trying to use the first callback function to save the data from the second CPT which is throwing errors in the foreach in the first callback function.
How can I tell the add_action function which callback to run when saving the data for the different CPTs?
A similar situation might also be causing the issues I’m having with the add_action(‘manage_posts_custom_column’) function call as well. And any info on how to get that to work would also be appreciated.
- The topic ‘Saving data for multiple custom post types’ is closed to new replies.