[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:
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:
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.
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.
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.
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?
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: