Skip Navigation

[Resolved] cred-draft posts left in database, coincidence with latest updates

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 6 replies, has 3 voices.

Last updated by svenA-3 1 year, 10 months ago.

Assisted by: Waqar.

Author
Posts
#2516787

I am using a frontend form for appointment creation in combination with a script hooked by cred_save_data_(form ID). It has worked fine over months and it still does work fine, but now with the latest updates I face draft post entries in my database with a creation date about 3 - 4 seconds after (!) creation of the intended appointment post. I have no idea why or where this post creation might be triggered.

Link to a page where the issue can be seen:
issue appears in database. The form works fine and the post created by it is in good shape.

I expected to see:
only the intended appointment post. No additional draft post

Regards,

Sven

The cred_save_data script of mine:


<h1> 
function g2TerminPersonNeu( $post_id, $form_data ){
	
	$startdateUNIX 			= $_POST[ 'wpcf-startdate' ][ 'timestamp' ];
	// $startdateUNIX		= $startdate[ 'timestamp' ];
	if ( empty( $startdateUNIX ) ) {
		wp_delete_post( $post_id, true );
		exit;
	}
	$enddate 			= $startdateUNIX + 3600;
	$topic 				= $_POST[ 'topic' ];
	$topic1 			= maybe_unserialize( $topic );
	if ( is_array( $topic1 ) && count( $topic1 ) > 1 ) {
		$topic2 		= implode( " | ", $topic1 );
	}
	else if ( is_array( $topic1 ) && count( $topic1 ) == 1 ) {
		$topic2			= $topic1[0];
	}
	else {
		$topic2 = $topic1;
	}
	
	$parentkartei 		= $_POST[ "connect-to-karteikopf" ];
	
	// set wpfd subcategories for termin related upload files
	$user_id = get_post_meta( $parentkartei, 'wpcf-mirror_id', true );
	$subcat1 = get_user_meta( $user_id, 'besch_id', true );
	$subcat2 = get_user_meta( $user_id, 'bef_id', true );
	$subcat3 = get_user_meta( $user_id, 'rech_id', true );
	
	
	// set termin post meta data
	$first = get_user_meta( $user_id, 'first_name', true );
	$last = get_user_meta( $user_id, 'last_name', true );
	$fullname = $first . ' ' . $last;
	$titleslug = strtolower( $first . '-' . $last );
	$Datum = date( 'd.m.Y', $startdateUNIX );
	$Zeit = date( 'H:i', $startdateUNIX );
	$Title =  'Termin für ' . $last . ', ' . $first . ' gebucht am ' . $Datum . ' um '. $Zeit .' Uhr';

	
	$values = array(
		'ID'			=> $post_id,
		'post_author' 	=> 1,
		'post_content'	=> $Title,
		'post_status'	=> 'private',
		'post_title'	=> $fullname,
		'post_name'		=> $titleslug,
		'post_type'		=> 'termin',
		'meta_input'	=> array(
			'wpcf-startdate'	=> $startdateUNIX,
			'wpcf-enddate'		=> $enddate,
			'wpcf-topic'		=> $topic2,
			'wpcf-calid'		=> 'keine',
			'wpcf-eterminid'	=> 'keine',
			'wpcf-terminstatus'	=> 1
		)
	);
	wp_update_post( $values, true, true );
		
	$wpfd_cat = g2SetupWPDownload_Taxonomy( $post_id, $subcat3, $subcat1, $subcat2, 0);
	$user_stuff = get_userdata( $user_id );
	$user_stuff->add_cap( "can_access_{" . $post_id . "}", true );
	
	// relate termin file to karteikopf parent
	toolset_connect_posts( 'karteikopf-termin', $parentkartei,  $post_id);
	$term = get_the_terms( $parentkartei, 'firmakat');
	$term1 = $term[ 0 ];
	$term_id = $term1 -> term_id;
	$employer = $term1 -> name;
	wp_set_post_terms( $post_id, $term_id, 'firmakat');
	
	//is termin for employer set too?
	if ( $employer != '00000' ) {
		// appointment date to german format without time
		$probstart = $startdateUNIX;
		$probstart = intval( $probstart );
		$probstart = date( 'd.m.Y', $probstart );
		// catch employer firma id via firmakat taxonomy term of current termin - karteikopf relationship
		$args = array(
			'post_type'		=> 'firma',
			'post_status'   => 'any',
			'fields'		=> 'ids',
			'meta_query' => array(
       			array(
           			'key'   => 'wpcf-kundennummer',
           			'value' => $employer,
       			)
    		)
   		);
		$firmaID = get_posts( $args );
		$post2_id = $firmaID[ 0 ];
		// firma --> karteikopf
		$firmkopf = toolset_get_related_post( $post2_id, 'karteikopf-firma');
		// karteikopf --> termine of employer
		$args = array(
			'query_by_role'	=> 'parent',
			'return'		=> 'post_id',
		);
		$firmtermine = toolset_get_related_posts( $firmkopf, 'karteikopf-termin', $args );
		$firmtermine = maybe_unserialize( $firmtermine );
		
		
		foreach ( $firmtermine as $firmentermin ) {
			$firmstart = get_post_meta( $firmentermin, 'wpcf-startdate', true );
			$firmstart = intval( $firmstart );	
			$firmstart = date( 'd.m.Y', $firmstart );		
			if( $firmstart == $probstart ) {
				$firmtopic = get_post_meta( $firmentermin, 'wpcf-topic', true );
				update_user_meta( 1, 'termcheck1', $firmstart . ' // ' . $probstart . ' // ' . $firmtopic );
				if ( $firmtopic == 'Untersuchungen'){
					$firmtermcat = get_post_meta( $firmentermin, 'beschterm_id', true);
					update_post_meta( $post_id, 'firmbeschtermid', $firmtermcat );
					
					exit;
				}
			}
		}
	}
}

add_action('cred_save_data_7149','g2TerminPersonNeu',10,2);
</h1>

#2518693

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

I scanned your code (rather than going through line-by-line in detail) to get a sense of what it does, which involves setting a parent Karteiköpfe post for the new Termine post (doing this manually, rather than letting Toolset Forms handle this), and some other functions relating to connected posts.

It's not obvious what is reponsible for what you describe.

But can you clarify, when you say draft posts appear in the database after the form has been submitted, are these draft Termine posts (the post type that is being created by the form) or something else? If so, do they have the same content (e.g. post title) and it is just the status that is different (draft rather than publish)?

#2519681

Hi Nigel, these cred-draft entries are Termine type posts and they have a "draft" title. The fun part is that the form first creates the Termine post exactly the way it should be and about 4 seconds later according to the database entries another Termine post appears in the database that has part of the data of the first post but without the title. Draft does NOT refer to the post status here.

In the end those draft posts aren't disturbing or interfering as far as I can tell, but they certainly fill the database over time without use.

Footnote: How would Forms handle the relationship setup for a Termine post if not via a script that provides the parent ID? Am I missing anything here? Leave me a note if this one requires a separate ticket as that hint somewhat took me by surprise.

#2520719

Hi Sven,

Thanks for writing back.

Do the posts you're referring to have the 'draft' status or the 'auto-draft' status? The revision or auto-save posts that WordPress creates have the 'draft' status, but the posts with the 'auto-draft' status are the ones that are created by the Toolset Forms.

A Toolset Form that creates a new post, makes a post with the 'auto-draft' status (and without any title) in the database before the form is submitted. On the form's successful submission, that post is replaced by the actual saved/published post, whereas another post with 'auto-draft' status is created for future use and this cycle continues.

I hope this explanation helps and since this is part of how Toolset Forms works in case of new posts, there is no option or workaround to safely disable the creation of these 'auto-draft' posts.

Footnote: For usual cases where a user can just select a related post from a post-relationship field in the form, you don't need the custom code with the 'toolset_connect_posts' function to programmatically join the posts. But for cases (like in this form), where you have the ID of the related field coming from some different source in the code, the custom code approach that you're using is the way to go.

regards,
Waqar

#2521267

Hi Waqar,

yes, they have auto-draft status. I understand correctly that my observation is normal behaviour with toolset and nothing to debug or worry about? In that case, my issue is solved.
Also thank you for the 'footnote' remarks.

best regards,

Sven

#2521309

You're very welcome and glad I could help.

Please mark this ticket as resolved and start a new one for each new question or concern.

#2521515

My issue is resolved now. Thank you!