Skip Navigation

[Resolved] Taxonomy terms of current page are not selected on CRED form anymore

This support ticket is created 6 years, 5 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 5 replies, has 2 voices.

Last updated by takoL 6 years, 5 months ago.

Assisted by: Beda.

Author
Posts
#903644

I am trying to:
automatically assign cred terms to newly created posts from a which on which the cred form resides. The page on which the cred form resides has the terms and previously the cred form would inherit these terms while creating a post from that page.

We have used this feature on many of our client's sites so now taxonomy terms are not assigned anymore, hence we have missing relationships between the posts concerned.

Is there a way to restore the this feature without having to change post type / form settings on all sites affected by this?

Thanks in advance!

#903736

The question is how you passed those Taxonomy Terms to the Forms until now.
Once I know how you populated the Toolset CRED Taxonomy Selectors - or programmatically updated the post, I can try to replicate this.

But it was never the behaviour that CRED just assigned the terms of the container page to the new or edited posts (which may be of another type).

Can you elaborate on the process so we can look into this?
If you use code, can you share the it?

#903749
toolset-parent-child-taxonomies-bad.png
toolset-parent-child-taxonomies-good.png

Hi Beda,

Thanks for the reply.

The situation is as follows.

I have a parent post type: Parent
I have a child post type: Child

Both post types have some taxonomies in common.

I have a CRED form that creates a child post.
I place this CRED form in a content template which displays the parent post.

It used to be so that the taxonomy fields of the Parent were automatically transferred to the input fields of the Child CRED form.
See the attachment "toolset-parent-child-taxonomies-good.png".

In some networks that I have this is no longer the case.
See the attachment "toolset-parent-child-taxonomies-bad.png".

The attachments are from different multi site installations.

I was wondering if this is a change in how CRED handles this type of inheritance.

Cheers,

Tako

#903885

It used to be so that the taxonomy fields of the Parent were automatically transferred to the input fields of the Child CRED form...
I was wondering if this is a change in how CRED handles this type of inheritance.

In my opinion, but let's see what you opt-in, this would be the wrong behaviour.

Just because accidentally the container post type shares the taxonomy does not mean any standard HTML form should get the form field values from that container post automatically.

Rather, yes, it should be possible to populate them with data from the current page (for example with ShortCodes in the value="" attribute of the CRED Form Fields ShortCodes)

Do you recall what version you updated?
Then I can verify that (yes, there where some BUG fixes on the current post id and current form id, which behaved wrongly).

#904233

I retested this once again, and unless we do populate the CRED Form Fields ShortCodes value attributes with ShortCodes, or update those with PHP hooking into (for example) cred_save_data(), then those Fields will be always empty on creation of a new post and always feature the values of the saved post if you edit a post.

This is how it should be, as well, as if you create a new post you do (primordially) not want it to be populated with other data.

It is the correct behaviour to not populate the fields automatically.

You will need to either pass a value to the Form Fields directly (if available) or update the value with PHP hooking into the cred_save_data():
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Taxonomies should be updated with the hook, rather than with ShortCode value since those fields do not feature a "value" attribute.

Hence you would use wp_set_post_terms() inside a cred_save_data() hook to update the taxonomies of the new or edited post with taxonomies of the current page (or any other post you choose in your code).
https://codex.wordpress.org/Function_Reference/wp_set_post_terms

An example code snippet that adds the term "my_term" (ID 5) of a hierarchical taxonomy "my_tax" to the post I submit with the current CRED Form.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)
    {
        wp_set_post_terms( $post_id, '5', 'my_tax', true);
    }
}

$post_id is the ID of the Post you edit or create with CRED and it's already provided by CRED in the variable.
wp_set_post_terms() as well offers $terms to pass, and $taxonomy + $append.
$terms is a comma-separated list of ID's of hierarchical terms or a comma-separated list of names of flat terms.

If you want to get the current page's terms (container ID of CRED), you can access them with wp_get_post_terms():
https://codex.wordpress.org/Function_Reference/wp_get_post_terms

Since CRED as well offers a Container ID variable, you can easily use that to access the terms of the page where the form is inserted to:

 wp_get_post_terms( $form_data['container_id'], 'my_tax');

$form_data['container_id'] in this case will give you the ID of the page where the form is inserted to.

If you require help for customizing the above Code samples for the site, I suggest to consult this with the Contractors:
https://toolset.com/contractors/

#904506

Hi beda, thanks for the reply.

I understand and actually agree that it is not good practice to automatically pre-populate input fields from a parent post.
Also, I can confirm that in the latest release of CRED (stable as well as beta) this is no longer the case.

Sad part for us is that prior to this version this seemed to be the case (and actually was quite convenient for us 🙂

We will have to change how we handle this in quite a few installations, so that is going to be some work.
But in the end it will be for the best.

Cheers,

Tako