Post version issues
-
I have posted quite a few times trying to resolve some problems related to saving custom posts and setting the $post_title automatically. I finally found a method that works rather well, but now I am having some issues with the publish and update processes.
The current code is as follows:
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && ($_POST['post_type'] == "sport_event")) { global $post; if(isset($post->ID)) { $title_ID = $post->ID; } else { unset($title_ID); } $event_team = $_POST["event_team"]; $event_opponent = $_POST["event_opponent"]; $event_date = $_POST["event_date"]; $my_post_title = ($event_team ." vs. " .$event_opponent ." - " .$event_date); $my_post_name = sanitize_title($event_team ."-vs-" .$event_opponent ."-" .$event_date); // Add the content of the form to $post as an array $custom_title = array( 'ID' => $title_ID, //Are you updating an existing post? 'post_content' => 'This is a custom post of the sport_event type. If you can see this text, something is wrong', //The full text of the post. 'post_name' => $my_post_name, // The name (slug) for your postet the status of the new post. 'post_title' => $my_post_title, 'post_type' => 'sport_event', 'post_status' => 'future', 'post_author' => 1 ); $title_meta_ID = wp_insert_post ($custom_title); add_post_meta($title_meta_ID, "event_team", $_POST["event_team"]); add_post_meta($title_meta_ID, "event_opponent", $_POST["event_opponent"]); add_post_meta($title_meta_ID, "event_date", $_POST["event_date"]); add_post_meta($title_meta_ID, "event_time", $_POST["event_time"]); add_post_meta($title_meta_ID, "territory", $_POST["territory"]); add_post_meta($title_meta_ID, "region", $_POST["region"]); add_post_meta($title_meta_ID, "game_audio", $_POST["game_audio"]); add_post_meta($title_meta_ID, "local_score", $_POST["local_score"]); add_post_meta($title_meta_ID, "opponent_score", $_POST["opponent_score"]); }
There is probably some optimization that could take place, but for the moment, my concern is that when I first publish it creates two entries. One is an Auto Draft with no information, and the other is the correct post with all desired fields set.
I am fairly certain I am not handling the post_ID correctly because if I edit a post and click update, it creates yet another Auto Draft and a new post with all the updated information. The total post count after an publish and update routine is now four.
In case it helps, here is some of the code related to the custom post:
add_action('init', 'sport_event_register'); function sport_event_register() { $labels = array( 'name' => _x('Sport Event', 'post type general name'), 'singular_name' => _x('Event', 'post type singular name'), 'add_new' => _x('Add New', 'event'), 'add_new_item' => __('Add New Event'), 'edit' => __( 'Edit' ), 'edit_item' => __('Edit Event'), 'new_item' => __('New Event'), 'view_item' => __('View Event'), 'search_items' => __('Search Event'), 'not_found' => __('Nothing found'), 'not_found_in_trash' => __('Nothing found in Trash'), 'parent_item_colon' => '' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, 'menu_icon' => get_stylesheet_directory_uri() . '/imgs/football.png', 'rewrite' => true, 'capability_type' => 'post', 'hierarchical' => false, 'menu_position' => null, 'supports' => array('') ); register_post_type( 'sport_event' , $args ); } register_taxonomy ("Teams", array("sport_event"), array("hierarchical" => false, "label" => "Teams", "singular_label" => "Team", "rewrite" => true)); register_taxonomy ("Opponents", array("sport_event"), array("hierarchical" => false, "label" => "Opponents", "singular_label" => "Opponent", "rewrite" => true)); add_action("admin_init", "admin_init"); function admin_init(){ add_meta_box("event_details-meta", "Event Details", "event_details", "sport_event", "normal", "low"); add_meta_box("event_teams-meta", "Event Teams", "event_teams", "sport_event", "normal", "low"); add_meta_box("event_post_game-meta", "Post Game", "event_post_game", "sport_event", "normal", "low"); } function event_teams() { global $post; $custom = get_post_custom($post->ID); $event_team = $custom["event_team"][0]; $event_opponent = $custom["event_opponent"][0]; $local_team = get_terms("Teams", "hide_empty=0"); $opponent = get_terms("Opponents", "hide_empty=0"); ?> <label>Team: </label> <select name="event_team"> <!-- Get all featured teams and display them as options --> <?php echo "<option style=\"background-color: #000; color: #fff;\" class='select_team' value='" . $event_team . "' " . ">" . $event_team . "</option>\n"; foreach ($local_team as $select_team) { echo "<option class='select_team' value='" . $select_team->name . "'>" . $select_team->name . "</option>\n"; } ?> </select> <label>Opponent: </label> <select name="event_opponent"> <!-- Get all opponents and display them as options --> <?php echo "<option style=\"background-color: #000; color: #fff;\" class='select_team' value='" . $event_opponent . "' " . ">" . $event_opponent . "</option>\n"; foreach ($opponent as $select_team) { echo "<option class='select_team' value='" . $select_team->name . "' " . ">" . $select_team->name . "</option>\n"; } ?> </select> <?php } function event_details(){ global $post; $custom = get_post_custom($post->ID); $event_date = $custom["event_date"][0]; $event_time = $custom["event_time"][0]; $territory = $custom["territory"][0]; $region = $custom["region"][0]; ?> <label style="font-weight:bold;">Event Date: </label> <input type="text" id="datepicker" name="event_date" class="date" value="<?php echo $event_date; ?>" /><br /> <label style="font-weight:bold;">Event Time: </label> <input type="text" id="timepickr" name="event_time" class="time" value="<?php echo $event_time; ?>" /><br /> <hr /> <label style="font-weight:bold;">Territory: </label><br /><?php $checked = $territory ?> <input type="radio" name="territory" value="home" <?php if ($checked == "home") echo "checked" ?>/> Home<br /> <input type="radio" name="territory" value="away" <?php if ($checked == "away") echo "checked" ?>/> Away<br /> <hr /> <label style="font-weight:bold;">Region: </label><br /><?php $checked = $region ?> <input type="radio" name="region" value="district" <?php if ($checked == "district") echo "checked" ?>/> District<br /> <input type="radio" name="region" value="non-district" <?php if ($checked == "non-district") echo "checked" ?>/> Non-district<br /> <?php }
Again, any insight would remove this one month roadblock from my to-do list.
- The topic ‘Post version issues’ is closed to new replies.