Skip Navigation

[Resolved] Auto create child post if generic checkbox is checked

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

Problem: I have a Form that creates parent posts. There is a generic checkbox in the Form. If the checkbox is checked, I would like to automatically create a child post.

Solution: Use the cred_save_data API to create a post automatically, use the toolset_connect_posts API to automatically link the new child post to the new parent post, and test the checkbox by testing if the slug key exists in the $_POST superglobal.

function create_child_log_item($post_id, $form_data) {
 
    if ( isset( $_POST['auto-add-log-item'] ) ) {
 
        $log_item = array(
            'post_title' => $title,
            'post_content' => '',
            'post_status' => 'publish',
            'post_type' => 'log-item',
            'meta_input' => array(
            ),
        );
 
        $log_item_id = wp_insert_post( $log_item , $wp_error );
 
        if ( $form_data["id"]==283 ) {
            toolset_connect_posts( 'maintenance-task-log-item', $post_id, $log_item_id );
        }
 
    }
 
}
add_action("cred_save_data","create_child_log_item",10,2);

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
https://developer.wordpress.org/reference/functions/wp_insert_post/

This support ticket is created 6 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by davidS-53 6 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1128190

I have a post type Maintenance Task that has a one => many relationship with post type Log Item.

I have a CRED form to create Maintenance Tasks. It's got a generic checkbox field on it with slug auto-add-log-item.

What I need to happen: when adding a Maintenance Task, and if the auto-add-log-item checkbox is ticked, I want to add a Log Item as a child.

Here's where my code is at the moment:

function create_child_log_item($post_id, $form_data) {

	if ( $_POST['auto-add-log-item'] != '' ) {

		$parent_id = get_the_ID();

		$log_item = array(
			'post_title' => $title,
			'post_content' => '',
			'post_status' => 'publish',
			'post_type' => 'log-item',
			'meta_input' => array(
			),
		);

		$log_item_id = wp_insert_post( $log_item , $wp_error );

		if ( $form_data["id"]==283 ) {
			toolset_connect_posts( 'maintenance-task-log-item', $parent_id, $log_item_id );
		}

	}

}
add_action("cred_save_data","create_child_log_item",10,2);

However, when submitting the form, I get a 500 error. Any idea why that might be?

#1128387

It could be because of this line:

$parent_id = get_the_ID();

Delete this line. The function get_the_id won't give you the information you need in this context. Use the $post_id variable instead (from the callback params). Then update your toolset_connect_posts API call:

toolset_connect_posts( 'maintenance-task-log-item', $post_id, $log_item_id );

If that does not resolve the problem, I'll need to take a look at the server logs to get more information about the error. If you're not familiar with server logs, I can show you how to activate them. Go in your wp-config.php file and look for

define('WP_DEBUG', false);

Change it to:

define('WP_DEBUG', true);

Then add these lines, just before it says 'stop editing here':

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

Create a new post to trigger the 500 error again. This should create an error_log.txt file in your site's root directory. Please send me its contents. Once that is done, you can revert the changes you made to wp-config.php.

#1128483

Ahh- thanks. It now works!

I did have to comment out the

if ( $_POST['auto-add-log-item'] != '' ) {

to get it to actually create a Log Item- so that line obviously isn't getting the value of the checkbox. Any idea why that may be?

#1129141

If it's a Types field, then you must add the wpcf- prefix to your slug:

if ( $_POST['wpcf-auto-add-log-item'] != '' ) {
#1129374

Hmm, doesn't work with or without the prefix. auto-add-log-item is a generic field- does that matter? Is the value not passed through or something?

#1129997

If it's a generic field, the wpcf- prefix should NOT be used. Sorry for the confusion on that. In the conditional you should test whether or not the key exists. It will not exist if the checkbox is unchecked:

if ( isset( $_POST['auto-add-log-item']  )  ) {
#1133763

Ahh right- thanks. All working now.