Skip Navigation

[Resolved] pre-select category or taxonomy in form

This support ticket is created 5 years, 1 month 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+01:00)

This topic contains 6 replies, has 3 voices.

Last updated by Nigel 5 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1606477

Hi there,
I have a post type Reviews and then another post type called 'Questions' with a relationship to 'Answers'

I'm putting the questions at the bottom of the post type 'reviews' so that people can ask questions in relation to the review, then people can answer the specific question.

My question is, if I put this form into the bottom of the review, is there anyway to pre-select the category or taxonomy that the question form is under?

thanks

#1606619

Here is the url if you want to see what I'm referring to:
hidden link

#1606835

There's no inbuilt method for it at the moment, but I've requested to look into possible implementations as the feature is often requested.

For now, you could do this with 2 different approaches, using Toolset and some Custom Code.

1. Using Custom JS to prepopulate the Form Field
https://toolset.com/forums/topic/default-value-select-taxonomy-term-in-cred/

2. Using Custom PHP hooked to cred_save_data, updating the Fields with the current posts' data.
Using PHP you can hook into cred_save_data() https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
This will provide you with a $post_id of the post edited or created with Forms and also a $container_id which is the ID of the post where that form is inserted to.

Armed with $container_id, which you can use in wp_get_post_terms() to retrieve the posts' terms of where you inserted the Form, you can then use wp_set_post_terms() to update the terms of the just created post.
Ref:
https://developer.wordpress.org/reference/functions/wp_set_post_terms/
https://developer.wordpress.org/reference/functions/wp_get_post_terms/

I will inform you here on eventual decisions made to implement such feature directly in Forms.
Please also don't hesitate to ask if anything is not clear about the implementation with Code. I can help with some examples and further explanation if required.
If you'd need full custom code assistance, we can recommend the Toolset Contractors here https://toolset.com/contractors/

#1607731

thanks so much. If you could give me an example that would really help me get my head around it.

thanks again.

#1608001

The example is involving both PHP and JS because you need the selection dynamic.

1. Create a Toolset Form, that features a Taxonomy Selector of select kind (in my example it is a DropDown single select)

2. Note down the Slug of your Taxonomy that is used in the Selector (in my case that is your-taxonomy-slug)

3. We create a custom shortcode to get the Term ID by Post ID. We add this code to the Theme's functions.php:

function ts_get_term_id_by_post_id( $atts = array(), $content = '' ) {
	$atts = shortcode_atts( array(
		'id' => '',
	), $atts, 'get-term-id-by-post-id' );

	foreach((get_the_terms($atts['id'], 'your-taxonomy-slug')) as $term) { return $term->term_id; }
}
add_shortcode( 'get-term-id-by-post-id', 'ts_get_term_id_by_post_id' );

We use WordPress ShortCode API as you can see, and this generates a ShortCode, which we can use later.
NOTE:
The above code only works if each post has ONE but no more term of the same taxonomy.
You must replace your-taxonomy-slug with the real slug of the taxonomy you use.

4. Insert a JS code like below in the Form's JS editor (just below the main drag and drop):

jQuery(document).ready(function($) {
	var theTermHTML = document.getElementById("theTermID");
  	var theTermID = theTermHTML.innerHTML;
    $("select[name^='your-taxonomy-slug[]'] option[value='"+theTermID+"']").attr("selected","selected");
});

You must replace your-taxonomy-slug with the real slug of the taxonomy you use.

5. Where you add the above Form (likely a View that lists all questions) you should (just before you add the Form) add this little HTML using our previously prepared ShortCode:

<div class="hidden" id="theTermID">[get-term-id-by-post-id id="[wpv-post-id]"]</div>

This will ensure, that the Term ID of the Post which should then be used for the form is actually added to the page.
It will not be visible as we have given it "hidden" class.
If your Website doesn't yet have it, you can add a CSS class for hidden in your Theme's CSS files, for example, like so:

.hidden {
display:none;
}

That's it.
Now, when you load the post (Review) where you have a lot of questions added (in a view) and each question has a reply form, that reply form will each have the same term selected, as the current Question of the current Review has.

This may sound a bit complicated, I suggest going thru above step by step and ask if you are not comfortable doing any of it, we can help with it.

#1610079

thank you that worked perfectly!

I tried adjusting it to get it to work with a checkbox but I couldn't make it come up as checked. is this possible or not possible with your solution?

thanks again for your help.

#1613909

Nigel
Supporter

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

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

Sorry for the delay, Beda is unavailable, let me step in here.

The selector for checkboxes would be different than for a select dropdown.

You would need to modify the relevant line like so:

$("input[name^='your-taxonomy-slug[]'][value='"+theTermID+"']").attr("checked","checked");