Skip Navigation

[Resolved] Writing URLPARAM to Taxonomy not working

This thread is resolved. Here is a description of the problem and solution.

Problem:

I am trying to have a form save the post taxonomy when the form is saved. The taxonomy slug is passed to the form via an URL parameter.

See details here:

https://toolset.com/forums/topic/writing-urlparam-to-taxonomy-not-working/#post-1232504

Solution:

It needs custom codes, see solution here:

https://toolset.com/forums/topic/writing-urlparam-to-taxonomy-not-working/#post-1232799

Relevant Documentation:

https://www.php.net/manual/en/reserved.variables.post.php

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

This topic contains 5 replies, has 2 voices.

Last updated by Luo Yang 5 years ago.

Assisted by: Luo Yang.

Author
Posts
#1232504

I am trying to have a form save the post taxonomy when the form is saved. The taxonomy slug is passed to the form via an URL parameter.

I created a generic form field to grab the taxonomy slug from the urlparam.

[cred_generic_field type='textfield' field='gen-doc-type' urlparam='doc-type-slug']
{
"required":0,
"default":""
}
[/cred_generic_field]

I then added the following function to be triggered on the form being saved.

add_action('cred_save_data', 'set_doc_type_tax',10,2);
function set_doc_type_tax($post_id, $form_data)
{
    // if a specific form for adding doc to hiring checklist form 5271

    if ($form_data['id']==5271)
    {
 
		$taxfield =  $form_data["gen-doc-type"]; // get taxonomy term slug from generic field
		$taxslug = 'document-type'; // taxonomy slug for worker document type
  
     wp_set_post_terms( $post_id, $taxfield, $taxslug ); 
    }
}

Nothing is being saved in the taxonomy and I can't figure out why.

#1232615

Hello,

Please check our document:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

There isn't such a parameter: $form_data["gen-doc-type"].

In your case, I suggest you try with PHP global POST variable, for example:
replace this line from:
$taxfield = $form_data["gen-doc-type"];

To:
$taxfield = $_POST["gen-doc-type"];

More help:
hidden link

#1232791

Thanks for the suggestion. I updated my code to the following:

add_action('cred_save_data', 'set_doc_type_tax',10,2);
function set_doc_type_tax($post_id, $form_data)
{
    // if a specific form for adding doc to hiring checklist form 5271
    if ($form_data['id']==5271)
    {
		$taxfield =  $_POST["gen-doc-type"]; // get taxonomy term slug from generic field
		$taxslug = 'document-type'; // taxonomy slug for worker document type
  
     wp_set_post_terms( $post_id, $taxfield, $taxslug ); 
    }
}

I added code to write the $taxfield and $taxslug to the error log and I can see they are the correct values. It appears wp_set_post_terms isn't setting the taxonomy. Do you have a suggestion?

#1232799

I found a solution. I changed it so wp_set_post_terms uses the ID not the SLUG. I found this suggestion in another support article here https://toolset.com/forums/topic/copy-post-taxonomy-value-to-another-taxonomy-on-cred-save/.

So the following code worked for me.

[php]
add_action('cred_save_data', 'set_doc_type_tax',10,2);
function set_doc_type_tax($post_id, $form_data)
{
// if a specific form for adding doc to hiring checklist form 5271
if ($form_data['id']==5271)
{
error_log("gen-doc-type=".$_POST["gen-doc-type"]);
$taxfield = $_POST["gen-doc-type"]; // get taxonomy term slug from generic field
$taxfield_term = get_term_by('slug', $taxfield, 'document-type');
$taxfield_id = $taxfield_term->term_id;
$taxslug = 'document-type'; // taxonomy slug for worker document type

wp_set_post_terms( $post_id, $taxfield_id, $taxslug );
}
}

#1232800

My issue is resolved now. Thank you!

#1233073

Thanks for sharing the solutions.

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