Sauter la navigation

[Résolu] Connecting Two CPT’s with intermediary CPT via CRED New Form

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
Connecting Two CPT's with intermediary CPT via CRED New Form

Solution:
You can use CRED hook "cred_save_data" to do any customization you
need and to connect the post using intermediary post entry.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/connecting-two-cpts-with-intermediary-cpt-via-cred-new-form/#post-871487

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This support ticket is created Il y a 6 années et 7 mois. 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

Auteur
Publications
#869198

So from this support post that Minesh was the support person for.
https://toolset.com/forums/topic/using-user-account-to-connect-member-profiles-with-articles-via-intermediary-cpt/

I need to be able to do the same thing but with a CRED New Post Form. The latter works only for existing CRED Post forms.

Thank you,
Ronald E

#871487

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - to adjust the code for your add form - I've added the generic field to your add form as given under:


[cred_generic_field field='_wpcf_belongs_member_id' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"persist":1,
"default":[0],
"options":[
{"value":"0","label":"Select Member Profile"},[wpv-view name="member-profile-list"]
]
}
[/cred_generic_field]

Then, I've adjusted the code that is added to current theme's functions.php as given under:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
function prefix_clean_view_output( $out, $id ) {
    if ( $id == '3679' || $id == '3633' ) {
        $start = strpos( $out, '<!-- wpv-loop-start -->' );
        if ( 
            $start !== false
            && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
        ) {
            $start = $start + strlen( '<!-- wpv-loop-start -->' );
            $out = substr( $out , $start );
            $end = strrpos( $out, '<!-- wpv-loop-end -->' );
            $out = substr( $out, 0, $end );
        }
    }
    return $out;
}

add_action('cred_save_data','func_add_writer',10,2);
function func_add_writer($post_id,$form_data) {
	global $current_user;
	
    if ($form_data['id']==3651) {
		
		// fetch existing writer posts and delete it
		$result = get_posts(array(
							'fields' => 'ids',
							'post_type' => 'writer',
							'meta_query' => array(
								array(
								   'key' => '_wpcf_belongs_article_id',
								   'value' => $post_id,
								   )
								   
							 )));
		
		if(count($result) > 0) {
			foreach($result as $k=>$v):
				wp_delete_post($v);
			endforeach;
		}
	
	}
	
	if ($form_data['id']==3651 or $form_data['id']==3633) {
	    // add new writer 
		  $data_arr = array(
				'post_title'=> "writer for article ".$post_id,
				'post_type' => 'writer',
				'post_status' => 'publish',
				'post_author' => get_current_user_id(),
   
                    );
       
		$insert_post_id = wp_insert_post($data_arr);
		
        update_post_meta($insert_post_id, '_wpcf_belongs_article_id',$post_id);
		update_post_meta($insert_post_id, '_wpcf_belongs_member_id',$_POST['_wpcf_belongs_member_id']);
        
    }
}

Could you please confirm it works for both of your add and edit form now.

#875162
minesh.gif

Thank you Minesh! This should be a feature of the Toolset Product. What you coded would be used by lots of people with Membership accounts wanting to link to articles via an intermediary post type relationship. What you did was make for a very nice user experience outside of WordPress with Toolset Cred and Access. Much better then haveing them go into the WP admin and find the right post type, scroll down to the bottom of the page, and then figure out how to use the relationship editor.

Thank you again and remember, contact me when you are in Seattle and we'll eat dinner in the Space Needle.