Skip Navigation

[Resolved] Pre Selected/Checked Taxonomy when creating a new post from current Post

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

Problem:
How to pre-select a taxonomy term in a post form?

Solution:
For custom fields you can preselect the value using the value attribute of the cred_field shortcode, but it doesn't work for taxonomies.

The client uses JS to automatically check one of the checkboxes. They wanted to get the value to be checked from the taxonomy assigned to the post where this form is displayed, i.e. using the wpv-post-taxonomy shortcode, but shortcodes are not expanded when adding code to the custom JS box.

That can be solved by including the JS in script tags within the form editor, like so:

<script>
jQuery(document).ready(function() {
    jQuery("input[data-value='[wpv-post-taxonomy type="car-brand" format="name"]']").prop("checked", true);
});
</script>

Client goes on to implement a solution with pre-selected select dropdowns, which requires registering a custom shortcode as well, as described here: https://toolset.com/forums/topic/pre-selected-checked-taxonomy-when-creating-a-new-post-from-current-post/#post-1107705

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

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 10 replies, has 2 voices.

Last updated by Nashaat 6 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1099768

Hi,

I have two CPTs one called "Cars" and one called "Ticket"

1- I have created a submission form for new "Tickets"
2- I have assigned values of some fields to pull information of the current "Car" post before submitting a new "Ticket"
3- I have added the Ticket form to the Cars Single Template and the values of those Fields are correctly pulled from the "Car" post
4- I can submit the form and i can see the values are stored in the post type "Ticket"

now the problem is with Taxonomies. Both CPTs are using the same Taxonomy (Car Brand) and i am trying to assign a value in the form field to be pre selected depending on which taxonomy is assigned to the car post. this means the form will select the Taxonomy term depending on the taxonomy value of the current "Car" Post. (for example if "Mercedes" in a "Car" post is assigned, then the Taxonomy field "Car Brand" in the Ticket form should select "Mercedes" automatically.

Value get selected right when its WPCF fields. Following shows the right value in the field of the form.

<p class='h5 font-weight-bold'>Model Year</p>
    [cred_field field='model-year' value="[types field='model-year'][/types]" urlparam='' select_text='--- not set ---' class='form-control' output='bootstrap']

BUT Taxonomies don't get selected. i tried following examples:


<p class='h5 font-weight-bold'>car brand</p>
     [cred_field field='car-brand'  display='select' single_select='true' output='bootstrap' value='[wpv-post-taxonomy type="car-brand" format="name"]']

     [cred_field field='car-brand_add_new' taxonomy='car-brand' type='add_new']


<p class='h5 font-weight-bold'>car brand</p>
     [cred_field field='car-brand'  display='select' single_select='true' output='bootstrap' value='[wpv-post-taxonomy type="car-brand" format="slug"]']

     [cred_field field='car-brand_add_new' taxonomy='car-brand' type='add_new']


<p class='h5 font-weight-bold'>car brand</p>
     [cred_field field='car-brand'  display='select' single_select='true' output='bootstrap' value='mini']

     [cred_field field='car-brand_add_new' taxonomy='car-brand' type='add_new']

Is there any way to manage this?
Thank you.

#1100514

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

Unfortunately, the value argument of the cred_field shortcode is only supported for custom fields, not for taxonomies.

We have a request to add support for taxonomies.

In the meantime for this to work you would need to use the Forms API.

Omit the taxonomy from the Ticket post form, and use the cred_save_data hook to trigger a code snippet that gets the parent car post, gets the term(s) set for the car brand taxonomy, and updates the ticket post with the same terms.

See:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/wp_get_post_terms
https://codex.wordpress.org/Function_Reference/wp_set_post_terms

#1105640

I have tried to do following for selecting the term in advance

jQuery(document).ready(function() {
    jQuery("input[data-value='bmw']").prop("checked", true);
});

This works but when the value is as giving above

i tried following but this didnt work.. you think there is a way to make it work with following jQuery

jQuery(document).ready(function() {
    jQuery("input[data-value='[wpv-post-taxonomy type="car-brand" format="name"]']").prop("checked", true);
});

could you please guide me more with the Cred hook if the jQuery method not possible..
thank you

#1106565

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

That could work, but the shortcode wouldn't be expanded if you are adding the JS in the JS editor (or in an enqueued external JS file).

To get it to work try adding the code directly in an editor inside script tags so that the shortcode is expanded, e.g.

<script>
jQuery(document).ready(function() {
    jQuery("input[data-value='[wpv-post-taxonomy type="car-brand" format="name"]']").prop("checked", true);
});
</script>
#1106612

Yes this works! can i do this also with select input instead checkbox?
for example this field

 [cred_field field='car-model' display='checkbox' output='bootstrap' display='select' single_select='true']
#1106637

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You have included the display attribute twice in that cred_field shortcode.

I think you should be able to do the same in principle.

Without setting up a test myself to confirm what the selector would be, you should find that with a select dropdown you can specify the selected option using jQuery val, e.g.

jQuery('#cred_field_selector').val( 'bmw' );

that would preselect the select field to the option with a value (not display text) of 'bmw'.

If you get stuck let me know and I'll take a closer look.

#1106775

I am not sure i understand exactly what i need to do.. i have tried following but didnt work....

[cred_field field='car-brand' output='bootstrap' display='select' single_select='true']

and then added following snippet:

jQuery(document).ready(function() {
  	jQuery('#cred_field_selector').val( 'renault' );
});

i am not sure what i am doing... i think i need to replace #cred_field_selector ?

#1107517

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Yes, I intended that you should check the source code and replace #cred_field_selector with a suitable selector to target the select dropdown.

Looking on my test site I think you can use something like the following:

jQuery( "[name='car-brand[]']" ).val(3);

Note that the options when you use a select field for the taxonomy have the term id as the value, not the slug, and you will need to set the id not the slug.

#1107569

Thanks Nigel this works now with the static term value!

this way i can select one of the terms.

jQuery(document).ready(function() {
  jQuery( "[name='car-brand[]']" ).val(47);
});

I have tried to to replace 47 with this code [wpv-post-taxonomy type="car-brand" format="id"] but this is not showing the value. how can i get the term id as the value?

#1107598

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Unfortunately the ID is not one of the available output formats for wpv-post-taxonomy: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-taxonomy

Which means you will need to register a custom shortcode to output it.

I don't have a test site with the same set up as you, so I haven't tested this, but I think you should be able to use this to output the id of the term (of the taxonomy you specify with the tax attribute) for the current post:

add_shortcode( 'termid', function( $atts ){

	global $post;
	$output = '';

	if ( isset($atts['tax']) ){

		$term_ids = wp_get_post_terms( $post->ID, $atts['tax'], array( 'fields' => 'ids' ) );

		$output = $term_ids[0];		
	}

	return $output;
});

Add that to your theme's functions.php file, and then use it like so:

[termid tax="car-brand"]

If there are more than one car-brand terms assigned to a post, it will output the id of the first one only.

#1107705

Thank you Nigel this has solved everything.

I have added your snippet to the function.php

add_shortcode( 'termid', function( $atts ){
 
    global $post;
    $output = '';
 
    if ( isset($atts['tax']) ){
 
        $term_ids = wp_get_post_terms( $post->ID, $atts['tax'], array( 'fields' => 'ids' ) );
 
        $output = $term_ids[0];     
    }
 
    return $output;
});

then i have added the jQuery in Toolset form Editor (not in the JS editor) with <script> tags like following:


<script>

jQuery(document).ready(function() {
  jQuery( "[name='car-brand[]']" ).val([termid tax="car-brand"]);
});

  
jQuery(document).ready(function() {
  jQuery( "[name='car-model[]']" ).val([termid tax="car-model"]);
});
  
  
jQuery(document).ready(function() {
  jQuery( "[name='car-version[]']" ).val([termid tax="car-version"]);
});

</script>