Skip Navigation

[Resolved] Post title set to 'Auto Draft'

This thread is resolved. Here is a description of the problem and solution.

Problem:
Updating the Post Title with the creed_save_data action does set the Post to "auto-draft" and does not properly update the title with the chosen Custom Field value.

Solution:
1. Make sure that CRED "Create new Post" Form is set to:
"Published" in the "Post Type Settings" section > "Select the status of content created by this form:"
in Dashboard > CRED > Forms > your_form > Edit

2. Insert this code to functions.php:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // Change your CRED Form ID
    if ($form_data['id']==ID)
 
    {
         //Declare the content of your variables, change Custom field slug accordingly
        $custom_title = get_post_meta( $post_id, 'wpcf-your_custom_field_slug', true );
 
        //collect data and define new title
        $my_post = array(
            'ID'           => $post_id,
            'post_title'   => $custom_title,
             
        );
 
        // Update the post into the database
        wp_update_post( $my_post );
 
    }
}

Documentation
https://toolset.com/documentation/user-guides/cred-api/#csd

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 5 replies, has 2 voices.

Last updated by Beda 6 years, 2 months ago.

Assigned support staff: Beda.

Author
Posts
#301480
Auto-Draft.jpg

I am building a site with custom post types for 'Artists' and 'Artwork' and integrating CRED forms so that artists can add additional work to the site for their page. In testing the CRED create form, new artwork is always added with a title of 'Auto Draft'. I should add that for certain reasons, I am not currently using the default WordPress title field but rather a Types field to define the "title" of the artwork. It's this custom field (wpcf-artwork-title) that I want to use to define the Post title when the CRED form is submitted.

I found the same question on this previous thread: https://toolset.com/forums/topic/cred-saving-user-submitted-custom-field-as-post-title/ but cannot get that same code to work. Here is the adjusted code for my purposes. Note that '301' is the ID of my create Artwork form.

add_action('cred_save_data_301','my_save_data_301_action', 5, 1);
function my_save_data_301_action($post_id) {
	$type = get_post_type($post_id);
	if ($type == 'item') {
		$title = get_post_meta($post_id, 'wpcf-artwork-title', true);
		$slug = sanitize_title($title);
		wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
	}
}
#301566

Thank you for contacting us here in the Support Forum

You can try to sue the CRED cred_save_data (action) as follows below, in conjunction with wp_insert_post function, and include the post_status value:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action( $post_id, $form_data ) {

        // if a specific form, change "ID" with your forms ID
        if ( $form_data['id'] == ID ) {
                
                //Declare the content of your variables
                $custom_content = get_post( $post_id );
                $custom_title = get_post_meta( $post_id, 'wpcf-artwork-title', true );//get your cf-value
                 
       
                //insert the post
                wp_insert_post( array(
                        'post_title' => $custom_title,//insert your custom title
                        'post_status' => 'publish',//save the post as published
                        'post_content' => $custom_content->post_content,//output the post body
                ) );
        } 
}

the above code needs to be adapted to your situation.

Please let me know if you have further questions regarding the issue mentioned in this Thread
and let me know if the above solution works for you, I look forward to your reply!

Thank you

#301605
Auto-Draft-Edit.jpg

Still no luck it seems. I edit the code above but only to include the form ID. I don't think I needed to customize anything else as you already included my custom field of 'wpcf-artwork-title'. However, I'm not sure if I need the 'post_content' in the 'wp_insert_post' array as I'm not utilizing the Editor box in my Custom Post Type settings. I also removed the comma at the end of 'post_content' as I don't think it's needed but it didn't make a difference either way. Including an additional screenshot of the Edit screen for the uploaded artwork. Any other thoughts?

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action( $post_id, $form_data ) {
    // if a specific form, change "ID" with your forms ID
    if ( $form_data['id'] == 301 ) {

            //Declare the content of your variables
            $custom_content = get_post( $post_id );
            $custom_title = get_post_meta( $post_id, 'wpcf-artwork-title', true );//get your cf-value


            //insert the post
            wp_insert_post( array(
                    'post_title' => $custom_title,//insert your custom title
                    'post_status' => 'publish',//save the post as published
                    'post_content' => $custom_content->post_content//output the post body
            ) );
    }
}
#301634

I apologize, please try this one (with wp_update_post):

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // Change your Form ID
    if ($form_data['id']==ID)

    {
         //Declare the content of your variables
        $custom_title = get_post_meta( $post_id, 'wpcf-artwork-title', true );

        //collect data and define new title
        $my_post = array(
            'ID'           => $post_id,
            'post_title'   => $custom_title,
            
        );

        // Update the post into the database
        wp_update_post( $my_post );

    }
}

Please make sure that your CRED "Create new Artwork" Form is set to:
"Published" in the "Post Type Settings" section > "Select the status of content created by this form:"
in Dashboard > CRED > Forms > your_form > Edit

I just tested above code with a simple Custom Post type + a simple CRED Form generating new "Artworks"

It substitutes the Post Title correctly with the Custom Field value.

Note that I assumed you set your Custom Post type to display the title (obvious) but removed the CRED form part to edit that Title.
(anyway it makes no difference on the above codes functionality)

Please let me know if that works

Thanks and please apologize the initial mislead!

#301831

Thank you, that did the trick! I also modified the if statement to include an OR for the edit version of the form. You may also want to update your code to not include the first 3 lines as I believe those are unnecessary.

if ($form_data['id']==301 || $form_data['id']==304)
#301871

Thanks for the catch!

I am happy you could solve the issue

Please feel free to come back at us with any more questions in new threads

Thank you