Skip Navigation

[Resolved] A form which allows me to add a product and connect a custom post type

This support ticket is created 6 years 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
- 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 8 replies, has 2 voices.

Last updated by Waqar 5 years, 12 months ago.

Assisted by: Waqar.

Author
Posts
#1153058

Tell us what you are trying to do?

I am trying to connect a child to it's parent using a generic field

[cred_generic_field field='@organization-campaign.parent' type='radio' class='test']
{
"required":1,
"validate_format":0,
"default":[],
"options":[ [wpv-view name="organization-select-generic" raw="true"] ]
}
[/cred_generic_field]
instead of using

[cred_field field='@organization-campaign.parent' class='form-control' output='bootstrap' select_text='--- not set ---']

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

add_action('cred_save_data','func_connect_child_posts',15,2);
function func_connect_child_posts($post_id,$form_data) {
    if ($form_data['id']==98) {

        toolset_connect_posts('organization-campaign',$_POST['@organization-campaign.parent'], $post_id);
    }
}
#1153096

Hey I found a solution :

add_action('cred_save_data', 'cred_custom_callback_fn',10,2);
function cred_custom_callback_fn($post_id, $form_data)
{
    $forms = array( 98 );
    // if a specific form
    if (in_array($form_data['id'], $forms))
    {
        if (isset($_POST['connect-posts']))
        {
            $parent_id = $_POST['connect-posts'];
            $parent = toolset_connect_posts( 'organization-campaign', $parent_id, $post_id );
            // your other callback code continues here
        }
    }
}

Do you know why this works???

Regards,
Nick

#1153159

Hi Nick,

Thanks for writing back and glad you've found the solution.

The code that you shared originally and the new one are both correct and should work.

The only thing that needs to be updated in your old code is the name of the generic field. Since you're using a custom generic field, you don't need to use the method/format of "@organization-campaign.parent" for the field's name.

You can update it to regular text name (e.g. "connect-posts") in the generic field's code and the hook's function and it will work.

I hope this clarifies.

regards,
Waqar

#1153205

My issue is resolved now. Thank you!

#1153233

Oh I forgot one thing. How would that work with toolset edit forms?

[cred_generic_field field='connect-organization-to-campaign' type='radio']
{
"required":1,
"validate_format":0,
"default":[ ],
"options":[ [wpv-view name="organization-select-generic" raw="true"] ]
}
[/cred_generic_field]

How do I make sure the right value from the create form is already selected when editing the post?

Thanks,
Nick

#1154427

Hi Nick,

There is no direct method for this and you'll need a couple of steps to achieve this in an edit form:

1. First, you'll need the value of the connected parent post in the edit form. You can add a new shortcode for this that first gets the ID of the current post which is being edited (assuming you're using the edit form on the same post which is being edited through the edit form) and then returns its connected parent post's ID:


add_shortcode( 'showparentpostid', 'showparentpostid_func');
function showparentpostid_func(){
	$postid = get_the_ID();

	$parentpost = toolset_get_related_posts( $postid, 'organization-campaign', 'child', 9999, 0, array(), 'post_id', 'other', null, 'ASC', true, $count );

	return $parentpost[0];
}

2. You can make this value available in the edit form through a hidden generic field:


[cred_generic_field type='hidden' field='parentpost']
{
"default":"[showparentpostid]"
}
[/cred_generic_field]

3. The last step would be to add some script to select the radio button, whose value matches the value of the new hidden generic field. In your edit form's "JS editor" tab, you can include:


jQuery( document ).ready(function() {
  var parentval = jQuery('input[name="parentpost"]').val();
  jQuery("input[name=connect-organization-to-campaign][value=" + parentval + "]").prop('checked', true);  
});

Note: For even more personalized assistance around custom code, you can consider hiring a professional from our list of recommended contractors at:
https://toolset.com/contractors/

regards,
Waqar

#1154434

Thanks Waqar,
yeah the thing is that I am not using the edit form on the same post which is being edited through the edit form but on a different page.

So the edit form has a dedicated page.

#1154435

Hm... I changed your shortcode to

add_shortcode( 'showparentpostid', 'showparentpostid_func');
function showparentpostid_func(){

	$product_id = get_post( get_the_ID() ); // Get ID of current product
  $auction_parent_page_id = toolset_get_related_post( // Get ID of parent auction page
    $product_id,
    'organization-campaign', //slug of relationship
    'parent'
);
return $auction_parent_page_id;

}

but I think it does the same thing.

#1156033

Hi Nick,

Thanks for sharing the update and I apologize for not able to follow up on this sooner.

I'll need access to the WordPress admin area to see where and how that edit form is being used.

Can you please share temporary admin login details in reply to this message, along with the details on where the edit form can be seen?

Your next reply will be private which means, only you and our support team will have access to it.

Important note: Please make a complete backup of your website, before sharing the login details.

regards,
Waqar