Skip Navigation

[Resolved] CRED parent post taxonomy value(s) to child post

This support ticket is created 8 years, 4 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
- 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/Hong_Kong (GMT+08:00)

Author
Posts
#354304
select-values.png

I am trying to: include the VALUE of the parent taxonomy in a child post created via a CRED post form

It seems that this should be a lot simpler than it is.

I have a CRED form to create child posts on the frontend.
This form is displayed in a view that displays the current parent.

I have managed to successfully create the child post using a hidden field by using:

		[cred_generic_field field="_wpcf_belongs_photo-competition_id" type="hidden" class="" urlparam=""]
{
"required":0,
"validate_format":0,
"persist":1,
"default":"_wpcf_belongs_photo-competition_id"
}
[/cred_generic_field]

... however, I want to also set the child taxonomies to those of the parent using hidden fields instead of allowing the user to choose from the global taxonomy fields. In this case, the parent is in 2 taxonomies.

Obviously CRED is pulling the (numerical) values in order to create the taxonomy select elements within the form, and I can display the selects within the form using:

[cred_field field='competition-year' display='select' single_select='true']
[cred_field field='competition-type' display='select' single_select='true']

When outputting to the page, the taxonomy values ARE shown in the select option field values - I need to include these values with hidden fields on form submission so that the created child post is in the same taxonomies as the parent - I do NOT want to hard code the values into a hidden field.

The fact that we cannot *easily* get and set values from parent posts is a serious limitation of CRED, in my opinion.

Thanks for any assistance

#354367

Unfortunately, upon further testing, I see that my previous code is not really working, either.
The CRED form creates the new post, but the hidden field does NOT set the parent when the post is added, so I have to go to the post in admin and manually choose the parent post.

... and for some reason, the form is not not generating a post slug based on the post title either.

#354460

Dear Thorworx,

1) For the hidden hidden field [cred-generic-field], you need to setup the parameter "default" as the parent post ID,
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred-generic-field

2) For taxonomy field, I suggest you try this:
update the taxonomy value of child post to what you want using CRED action hook:
cred_save_data: (action)
Hook to do custom action when post data are saved to database.
https://toolset.com/documentation/user-guides/cred-api/#csd

#355359

Thanks for replying luoy,

I've looked at the CRED action hook, but I'm not sure how I can pass the VALUE of the parent post's taxonomies to it. I don't want to have to hardcode the value in a hidden field, because this form will be used dynamically based on the current parent.

I'm currently calling the CRED form by using the form link method which passes the parent id on the url like this:
?parent_my-cpt_id=85

.... but I don't see how I can get the taxonomies of the parent on the form page.

Do I need to do some sort of meta query to get the value before using the cred_save_data action hook?
Should I create a View or content template and include the form directly?

Thanks for any assistance

#355378

It needs custom PHP codes, could you duplicate same problem in a test site, and fill below private detail box with login details and ftp access, also point out the problem CRED form URL and page URL, where I can edit the custom PHP codes, I need test and debug in a live website. thanks

#355530

Thanks for the details, I can log into your website, but it is not an admin account, I can not see how do you setup the CRED form, I need an admin account to test and debug in your test site.

#355821

Thanks for the details, I am testing in your website, will feedback if there is any found.

#356052

I have added below codes in your theme/functions.php:

add_action('cred_save_data', 'parent_taxonomy_func',10,2);
function parent_taxonomy_func($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==11)
    {
        $parent_competition_category_id = get_post_meta($post_id, '_wpcf_belongs_competition-category_id', true);
		
		if($parent_competition_category_id){
			$competition_type_obj = get_the_terms( $parent_competition_category_id, 'competition-type' );
			$competition_type_ids = array();
			foreach($competition_type_obj as $v){
				$competition_type_ids[] = $v->term_id;
			}
			wp_set_object_terms( $post_id, $competition_type_ids, 'competition-type', false );
			
			$competition_year_obj = get_the_terms( $parent_competition_category_id, 'competition-year' );
			$competition_year_ids = array();
			foreach($competition_year_obj as $v){
				$competition_year_ids[] = $v->term_id;
			}
			wp_set_object_terms( $post_id, $competition_year_ids, 'competition-year', false );
		}
    }
}

Please test again, check if it is what you needed.

#358097

Luoy,

Thanks for the help. It looks like this is going to work. I will need to look at tweaking it a bit to set the correct taxonomies properly.. but I think I should be able to figure it out from your code.

If I run into trouble, I will post a new thread.
Thanks again

#505693

I've started using the above code in my project... however there is a problem.

The code is assigning ALL of the taxonomy terms to the post - NOT just the one that is selected in the parent.

I assume this is happening in the foreach loop where it's getting ALL the IDs as an array but then the "wp_set_object_terms" is not setting the parent's chosen term ID, it's setting ALL terms.

Can you give me an idea on how to set the child's taxonomy to ONLY the term set by the parent?

Thanks

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.