Custom Post Custom Taxonomy Data Not Saving in Edit Screen

I have a custom post type “projects” with a custom taxonomy “project_type.” If I select a project type taxonomy within the post editor screen and hit update, the taxonomy isn’t updated. Worse yet, it clears all taxonomy values associated with that project. Oddly, I added a column in the admin screen to display the custom taxonomy values and I am able to update the taxonomy values via WP’s built-in quick edit feature.

Here is the bulk of the custom post type and taxonomy code: http://pastebin.com/2TdmVEAE

So, I’m thinking I need some function that will amend the save post function to include the custom taxonomy data. Right now the save_post function must not include custom taxonomies in a custom post type, and that is why it is clearing the data. (side: why does it work with quick post?)

I just don’t know how exactly to handle the data in the custom taxonomies to compare/write the selected taxonomies to the specific post. I’ve just discovered the codex on save_post… Here’s what I have so far, built off another post that is similar:

add_action('save_post', 'dcg_taxonomy_save');

// Save data from meta box
function dcg_taxonomy_save($post_id) {
global $post;

// verify nonce
if (!wp_verify_nonce($_POST['project_type'], basename(__FILE__))) {
    return $post_id;
}

// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return $post_id;
}

// check permissions
if ('page' == $_POST['project']) {
    if (!current_user_can('edit_page', $post_id)) {
        return $post_id;
    }
} elseif (!current_user_can('edit_post', $post_id)) {
    return $post_id;
}

$old = wp_get_post_terms($post_id, 'project_type', array("fields" => "all") ); // grabs all terms for this post in the taxonomy
$new = $_POST[ ...?... ]; // <-- how do I grab the custom taxonomy values

// probably need to run a foreach through the array of taxonomy values, yea?
if ($new != $old) {
    wp_set_post_terms($post_id, $new, 'project_type', false ) // sets new taxonomy terms
}
}

1 Answer
1

If you have a custom post type and a custom taxonomy assigned to it, you do not need to handle the saving of those terms yourself, WordPress already does this for you. So you can remove your taxonomy save code.

Also you should put your register_post_type and custom taxonomy registering code inside a function, and attach it to the init hook. It’s not safe to just declare them outright.

Leave a Comment