Skip Navigation

[Resolved] Create post with automatic taxonomy term using CRED

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

Problem: I have 3 CRED forms that create new posts in the same post type. I would like to automatically assign one term to each new post, and I would like that term to be different for each form. I tried using the "value" attribute of the cred_field shortcode, but it does not appear to save the correct term information.

Solution: The "value" attribute of a cred_field with single_select does not apply to taxonomy term fields. Instead, you can do something similar with the CRED API:

add_action('cred_save_data', 'auto_cred_term_action',10,2);
function auto_cred_term_action($post_id, $form_data)
{
    // form #1
    if ($form_data['id']==123)
    {
  
            $tag = array( 11 ); // Taxonomy term ID
            $taxonomy="storytypes"; // Taxonomy name
            wp_set_post_terms( $post_id, $tag, $taxonomy );
  
    }
    // form #2
    if ($form_data['id']==234)
    {
  
            $tag = array( 22 ); // Taxonomy term ID
            $taxonomy="storytypes"; // Taxonomy name
            wp_set_post_terms( $post_id, $tag, $taxonomy );
  
    }
    // form #3
    if ($form_data['id']==456)
    {
  
            $tag = array( 33 ); // Taxonomy term ID
            $taxonomy="storytypes"; // Taxonomy name
            wp_set_post_terms( $post_id, $tag, $taxonomy );
  
    }
}

Remove the taxonomy cred_field shortcode from the form, and modify the form and term IDs as needed in the code above.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/wp_set_post_terms
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_field

This support ticket is created 6 years, 7 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 10 replies, has 2 voices.

Last updated by Christian Cox 6 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#757913

Hi there,

I have a custom post type with a custom taxonomy.

The post type is "Your Stories" and the taxonomy is "Story Types".

There are three terms in the taxonomy, and I need to create a different front end form for each.

CRED Form one should submit posts to the Your Stories post type, in Story Type A.

CRED Form one should submit posts to the Your Stories post type, in Story Type B.

CRED Form one should submit posts to the Your Stories post type, in Story Type C.

I cannot find a way to make a CRED form only submit posts to a certain taxonomy term.

Please can you help me with this?

Many thanks for your time,
Andrew.

#763449
Screen Shot 2018-04-29 at 3.37.41 PM.png

Hi, you can pre-define a value for the taxonomy CRED field, and hide it from the front-end User. For example, here is a hidden taxonomy field that predefines the term "nonfiction" for my taxonomy "book-tax":

<div style="display:none;">
[cred_field field='book-tax' display='select' single_select="true" output='bootstrap' value="25"]
</div>

Notice that the nonfiction term has an ID of 25 in the attached screenshot. You can find term IDs by hovering over the term in the term dashboard. So if you create 3 different CRED forms, you can place your 3 different term IDs in the respective form.

Let me know if you have questions about this.

#771691

Hi Christian,

Many thanks for your help.

I just tried that, and it almost works but not quite.

The taxonomy is "storytypes" and the id of the term I want to assign it to is 11. So I added the code as follows:

<div style="display:none;">
[cred_field field='storytypes' display='select' single_select="true" output='bootstrap' value="11"]
</div>

But instead of adding a test post to category with ID 11, it created a new category in the "storytypes" taxonomy, named "11" and assigned the new post to that category.

Do I need to refine the code a little?

Many thanks again for your time.

Very best wishes,
Andrew.

#772111

...more information after my reply a few minutes ago.

What seems to be happening is that each test post is being created with the first term in the dropdown chosen as the term the post is submitted with. So the hidden single select field is being created, but the term id is not being applied.

#773100

Okay please answer some more questions:
Is it possible you have more than one CRED field for the storytypes taxonomy in this form?
What do you see when you go to this URL?
yoursite.com/wp-admin/term.php?taxonomy=storytypes&tag_ID=11

#773274
Screen Shot 2018-04-30 at 21.16.35.png

Hi Christian,

I see what is shown in the attached screenshot.

There is only one CRED field for the storytypes taxonomy on the form.

Here is the entire form code:

[credform class='cred-form cred-keep-original']

	[cred_field field='form_messages' value='' class='alert alert-warning']

    <div style="display:none;">
      [cred_field field='storytypes' display='select' single_select="true" output='bootstrap' value="writing-and-me"]
    </div>

	<div class="form-group">
		<label>Your Story Name</label>
		[cred_field field='post_title' post='tellyourownstory' value='' urlparam='' class='form-control' output='bootstrap']
	</div>

	<div class="form-group">
		<label>Your Story Description</label>
		[cred_field field='post_content' post='tellyourownstory' value='' urlparam='' output='bootstrap']
	</div>


	<div class="form-group">
		<label>Featured Image</label>
		[cred_field field='_featured_image' value='' urlparam='' output='bootstrap']
	</div>

	<div class="form-group">
		<label>Name of Writer</label>
		[cred_field field='ecpt_field_yourstory_name' post='tellyourownstory' value='' urlparam='' class='form-control' output='bootstrap']
	</div>

	<div class="form-group">
		<label>Information about the writer</label>
		[cred_field field='ecpt_field_yourstory_info' post='tellyourownstory' value='' urlparam='' class='form-control' output='bootstrap']
	</div>

	[cred_field field='form_submit' value='Submit' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']

[/credform]
#773316

It looks like you have the term slug here instead of the term ID:

[cred_field field='storytypes' display='select' single_select="true" output='bootstrap' value="writing-and-me"]

This is probably why the first option is always being selected - there is nothing with the value "writing-and-me" in the list to choose from. Please change this to value="11" and reload the CRED form. Are you familiar with the browser inspector? If so, please inspect the hidden select field when you reload the page. Copy + paste the markup for the entire select field and all options here for me to review.

#775618
Screen Shot 2018-05-01 at 07.07.13.png

Hi Christian,

I had changed that to the term slug to try, as when I tried the term ID it didn't work. So I've tried both ways.

Here is the code from the inspector (also as a screenshot attached)...

<div style="display:none;">
    <div class="js-wpt-field-items js-wpt-repetitive wpt-repetitive" data-initial-conditional="" data-item_name="taxonomyhierarchical-storytypes">
<select id="cred_form_43031_1-select-1-1525154646" class="wpt-form-select form-select select" data-wpt-type="select" name="storytypes[]">
<option value="9" data-parent="-1" class="wpt-form-option form-option option" data-wpt-type="option" data-wpt-id="cred_form_43031_1_cred_form_43031_1-select-1-1525154646" data-wpt-name="storytypes[]">Mining Memories</option>
<option value="13975" data-parent="-1" class="wpt-form-option form-option option" data-wpt-type="option" data-wpt-id="cred_form_43031_1_cred_form_43031_1-select-1-1525154646" data-wpt-name="storytypes[]">Monday Miscellany</option>
<option value="1513" data-parent="-1" class="wpt-form-option form-option option" data-wpt-type="option" data-wpt-id="cred_form_43031_1_cred_form_43031_1-select-1-1525154646" data-wpt-name="storytypes[]">Tell Your Own Story</option>
<option value="11" data-parent="-1" class="wpt-form-option form-option option" data-wpt-type="option" data-wpt-id="cred_form_43031_1_cred_form_43031_1-select-1-1525154646" data-wpt-name="storytypes[]">Writing & Me</option>
</select>

<input type="hidden" id="cred_form_43031_1-hidden-1-1525154646" name="storytypes_hierarchy" value="" class="wpt-form-hidden form-hidden" data-wpt-id="cred_form_43031_1_cred_form_43031_1-hidden-1-1525154646" data-wpt-name="storytypes_hierarchy"></div>
</div>
#779352

I apologize, I gave you the wrong information. It turns out that the value attribute does not apply for taxonomy fields in CRED. Instead, the best way to accomplish this is to use the CRED API. Please add this code to your child theme's functions.php file:

add_action('cred_save_data', 'auto_cred_term_action',10,2);
function auto_cred_term_action($post_id, $form_data)
{
    // form #1
    if ($form_data['id']==123)
    {
 
            $tag = array( 11 ); // Taxonomy term ID
            $taxonomy="storytypes"; // Taxonomy name
            wp_set_post_terms( $post_id, $tag, $taxonomy );
 
    }
    // form #2
    if ($form_data['id']==234)
    {
 
            $tag = array( 22 ); // Taxonomy term ID
            $taxonomy="storytypes"; // Taxonomy name
            wp_set_post_terms( $post_id, $tag, $taxonomy );
 
    }
    // form #3
    if ($form_data['id']==456)
    {
 
            $tag = array( 33 ); // Taxonomy term ID
            $taxonomy="storytypes"; // Taxonomy name
            wp_set_post_terms( $post_id, $tag, $taxonomy );
 
    }
}

Change 123, 234, and 456 to match the 3 form IDs, and change 11, 22, and 33 to match the term IDs you want to add to each post.

#779744

Thank you, this looks like it's working. Am going to do some more tests.

Is it possible to save to two taxonomy terms for one form? E.g. can I comma separate, for example. the term ID's that I want to save to?

#779890

Correct, the $tag array can hold a comma-separated list of term IDs, like this:

$tag = array( 33, 44, 55 );

More information about this WordPress API can be found here:
https://codex.wordpress.org/Function_Reference/wp_set_post_terms