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 );
}
}
}
Hi Aaron,
Is the $privacy_status a created taxonomy ?
Meaning the value that is here does it have a corresponding value on the taxonomies or is it that you're creating the term here.
However the correct function to use for setting the taxonomy on the post would be.
https://developer.wordpress.org/reference/functions/wp_set_post_categories/
Thanks,
Shane
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.
add_action('cred_save_data', 'update_crowd_privacy_category',10,2);
function update_crowd_privacy_category($post_id, $post_categories)
{
if ($form_data['id']==20) {
{
$privacy_status = $_POST['wpcf-crowd-privacy-2'];
$my_post = array(
'ID' => $post_id,
'post_categories' => $privacy_status
);
wp_update_post( $my_post );
}
}
}
Hi Aaron,
As mentioned previously to set the post taxonomies you will need to use the function in the link below.
https://developer.wordpress.org/reference/functions/wp_set_post_categories/
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.
Thanks,
Shane
I am not proficient enough with this code to do I what I need it to do. I appreciate your guidance.
Hi Aaron,
In this case try using this code below.
add_action('cred_save_data', 'update_crowd_privacy_category',10,2);
function update_crowd_privacy_category($post_id, $post_categories)
{
if ($form_data['id']==20) {
{
$privacy_status = $_POST['wpcf-crowd-privacy-2'];
wp_set_post_categories($post_id, $privacy_status);
}
}
}
Please let me know if this helps.
Thanks,
Shane
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.
Hi Aaron,
It could be a problem where the ids aren't matching.
Would you mind allowing me to have admin access to the website so that I can have a more detailed look at this for you ?
Please where applicable please provide me with a link to an example page where I can see the issue.
I've enabled the private fields for your next response.
Thanks,
Shane
Hi Aaron,
Thank you for the credentials but this account doesn't give me access to the admin panel.
Can you update the user to an admin account.
Thanks,
Shane
Sorry about that. Admin privileges have been added.
Hi Aaron,
I see the problem.
The custom field ids do not match the term ids/
Currently from what I see the Term IDS are as follow
Public = 1
Private = 13
Unlisted = 12
Inactive = 11
Changing your select field values to these should now cause the code to work correctly.
Thanks,
Shane
It seems to have the same behavior. Do I need to specify to change the category for the Custom Post Type or does post_id already handle that?
Hi Aaron,
Can you let me know the exact page that the form is on so that I can do some testing on this as well.
Thanks,
Shane
If you log in then go to this link: lien caché
then click "Edit Crowd", that is the form where you can set the privacy.
Hi Aaron,
This should be working now.
The problem was with your function.
Specifically this line below.
function update_crowd_privacy_category($post_id, $post_categories)
$post_categories is not a valid parameter for the function, this should be replaced with $form_data.
Once I did this the function started to work as intended.
Thanks,
Shane