Tell us what you are trying to do?
What am I doing wrong here? I created this snippet to update the category for my custom posts based on a custom field, but it doesn't seem to be updating the category. I troubleshooted it by updating the post_title based on the entry and it works, but it won't update the post_category. Any guidance would be greatly appreciated:
add_action('cred_save_data', 'update_crowd_privacy_category',10,2);
function update_crowd_privacy_category($post_id, $form_data)
{
// Change the ID below to the ID of your Toolset Form
if ($form_data['id']==20) {
//if our checkbox field checkbox_yes_protect_my_post is checked
{
//Get the value from post_password_custom field
$privacy_status = $_POST['wpcf-crowd-privacy-2'];
//update the post with the new password
//we can use Update, because at this point forms already created the post
//$post_id is the Post we create with Forms, it is given to us by the Forms API, see functions parameters
$my_post = array(
'ID' => $post_id,
'post_category' => $privacy_status
);
// Insert the post into the database
wp_update_post( $my_post );
}
}
}
I changed the code to what I have below.
I have a custom field called "wpcf-crowd-privacy-2" and there are 4 options with their own slugs (1, 2, 3, and 4). Then I'm using the default categories within my custom post type with the same 4 options. I am trying to update the category for the matching slug from the wpcf-crowd-privacy-2 field when the cred form is submitted.
There is no post_categories attribute for the wordpress post object as depicted by the object attributes below.
object(stdClass)
public 'ID' => int
public 'post_author' => string
public 'post_date' => string
public 'post_date_gmt' => string
public 'post_content' => string
public 'post_title' => string
public 'post_excerpt' => string
public 'post_status' => string
public 'comment_status' => string
public 'ping_status' => string
public 'post_password' => string
public 'post_name' => string
public 'to_ping' => string
public 'pinged' => string
public 'post_modified' => string
public 'post_modified_gmt' => string
public 'post_content_filtered' => string
public 'post_parent' => int
public 'guid' => string
public 'menu_order' => int
public 'post_type' => string
public 'post_mime_type' => string
public 'comment_count' => string
public 'filter' => string
So setting the taxonomy term to the post will require the use of the appropriate function.
It is affecting the category by adding it to the default category if it is not already assigned to a category. It doesn't update the category for the slugs that match the custom field slug though.