2

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
}
}
0

1 Answer 1

0

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.

12
  • Thanks for the tips! Yea, I have them all registered, but on update/publish it clears the taxonomy data - so something isn't working correctly, hence my code above as an attempt to fix it. Apr 5, 2012 at 18:35
  • I would put your registration code into functions, it may be that your getting errors or warnings because the registration functions aren't defined when the calls are made during the quick edit ajax requests
    – Tom J Nowell
    Apr 5, 2012 at 18:36
  • Also move your permalink structure stuff into the appropriate hook so it doesn't redo the same work each time you load a page, and see if the problem goes away when you comment out your metabox code
    – Tom J Nowell
    Apr 5, 2012 at 18:38
  • Ok, I moved the registration stuff into a function, hooked in with init, but still same error. I'm not sure they are related... pastebin.com/1y1qM5ZJ Apr 5, 2012 at 19:09
  • 1
    i have the exact same issuse ;( - with you had shared the solution you found. i see its 2012 - but.. do you remember what was the issuse?
    – Sagive
    Dec 10, 2014 at 0:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.