Home › Toolset Professional Support › [Resolved] how to exclude a single taxonomy entry from the Post Form.
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 |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10: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/Kolkata (GMT+05:30)
This topic contains 13 replies, has 2 voices.
Last updated by francescoG 1 year, 10 months ago.
Assisted by: Minesh.
Tell us what you are trying to do?
Hi,
I have a problem that I cannot solve, I hope you can help me.
I created an “Online Application Form”.
I use the Toolset together with Elementor pro (to create front-end templates).
With Toolset:
I created a post type called “Candidate” and a
taxonomy called “Job Positions”
and several “custom fields”
I created a Post Form where I inserted the "job Position" taxonomy field.
Each single entry corresponds to a vacant Job, I would like to be able to exclude the Jobs that are no longer vacant from the taxonomic list displayed in the Post Form. I want to exclude them only in the Post Form.
In the Post Form I want to show only vacant jobs. How can I proceed?
Thanks in advance for your help.
Francesco
Hello. Thank you for contacting the Toolset support.
Can you please share screenshot of your form or maybe share the link of the form where you added the form and tell me what term you want to exclude?
Hi, Thanks for the reply.
Link of my CRED form:
hidden link
I tried the solution i found here:
https://toolset.com/forums/topic/restrict-user-to-select-one-category-only/
1. I have divided the individual categories (Taxonomie= “Stellen”) into two parent categories: “Aktiv” and “Nicht Aktiv”
2. I created a view of my taxonomy. In the Query Filter section, I filtered by the main taxonomy "Active".
3. I added the following code to my theme's functions.php file (I replaced "12345" with the numeric id “1730” of the newly created view):
add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 10, 3);
function prefix_clean_view_output( $out, $id ) {
if ( $id == '1730' ) { //Please adjust to your Views ID
$start = strpos( $out, '<!-- wpv-loop-start -->' );
if (
$start !== false
&& strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
) {
$start = $start + strlen( '<!-- wpv-loop-start -->' );
$out = substr( $out , $start );
$end = strrpos( $out, '<!-- wpv-loop-end -->' );
$out = substr( $out, 0, $end );
} else {
$start = strpos( $out, '>' );
if ( $start !== false) {
$out = substr( $out, $start + 1 );
$end = strpos( $out, '<' );
$out = trim(substr( $out, 0, $end ));
}
}
}
return $out;
}
4. I deleted all the code in the View "1730"´s loop output editor. And pasted the following:
[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-item index=1]{"value":"[wpv-taxonomy-slug]","label":"[wpv-taxonomy-title]"}[wpv-item index=other],{"value":"[wpv-taxonomy-slug]","label":"[wpv-taxonomy-title]"}</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found][wpml-string context="wpv-views"]No items found[/wpml-string][/wpv-no-items-found][wpv-layout-end]
5. I have inserted in my CRED form editor a generic "Select" field. I gave the field a name (aktiv-stelle) (Label: “Offene Stellen *”) and where it says "Use a shortcode instead of options" I pasted in the shortcode to display the view I've been working on:
[wpv-view name="view-aktiv-stellen"]
6. In the front end of the CRED form the field "Offene Stellen *" appears with only the sub-categories of the parent category "Aktiv". (The field "Offene Stellen *" appears when in the field "Initiativ-Bewerbung*" there is the choice "Nein")
Perfect!!!
There's just one big problem:
When I choose a subcategory and submit the CRED Form the subcategory does not appear in the backend: the category field is empty.
where did I go wrong? What should I do to make the choice made in the frontend CRED module appear in the backend as well?
Thank you very much for helping.
Francesco
The step #3 code is not required if you edit your view and see the the following checkbox "Disable the wrapping DIV around the View".
If you see that you should checkmark that checkbox and remove the code you added at step #3. Do you see same results? If yes, that code you added is not required anymore.
It seems you will require to use the Toolset form's hook "cred_save_data" to set the selected term.
For example:
=> You can add the following code to "Custom Code" section offered by Toolset:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
add_action('cred_save_data', 'func_set_terms_on_submit',10,2); function func_set_terms_on_submit($post_id, $form_data) { // if a specific form if ($form_data['id']==99999) { $terms = $_POST['your-term-select-field-slug']; wp_set_object_terms($post_id, $terms, 'taxonomy-slug'); } }
Where:
- Replace 99999 with your original Form ID
- Replace 'your-term-select-field-slug' with your original field slug that holds the terms
- Replace 'taxonomy-slug' with your original taxonomy slug
More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
=> https://codex.wordpress.org/Function_Reference/wp_set_object_terms
Hi Minesh,
Thanks for the reply.
I replaced 99999 with my original module ID 753
I replaced 'taxonomy-slug' with my original taxonomy slug 'stelle'
add_action('cred_save_data', 'func_set_terms_on_submit',10,2);
function func_set_terms_on_submit($post_id, $form_data) {
// if a specific form
if ($form_data['id']==753) {
$terms = $_POST['your-term-select-field-slug'];
wp_set_object_terms($post_id, 'term-slug', 'stelle');
}
}
- Replace 'your-term-select-field-slug' with your original field slug that holds the terms.
I don't understand where should I get my original field slug:
$terms = $_POST['your-term-select-field-slug'];
This is the taxonomy permalink:
hidden link
Can you help me thanks
Francesco
Can you please share the link of the page where you added the form where I can see the select field you added.
hidden link
Can you please try to use the following code and check if that help you to resolve your issue:
add_action('cred_save_data', 'func_set_terms_on_submit',10,2); function func_set_terms_on_submit($post_id, $form_data) { // if a specific form if ($form_data['id']==753) { if(isset($_POST['aktiv-stelle']) and $_POST['aktiv-stelle']!='' ){ $terms = $_POST['aktiv-stelle']; wp_set_object_terms($post_id, $terms, 'stelle'); } } }
Hi Minesh,
Thanks for the reply.
I entered the code, but it doesn't work and it gives me an error.
Thanks for your help and your patience.
Francesco
Seems you removed the security line.
Can you try to add the code as given under and try to save the snippet again by clicking on the button "save snippet and try again":
<?php /** * New custom code snippet (replace this with snippet description). */ toolset_snippet_security_check() or die( 'Direct access is not allowed' ); add_action('cred_save_data', 'func_set_terms_on_submit',10,2); function func_set_terms_on_submit($post_id, $form_data) { // if a specific form if ($form_data['id']==753) { if(isset($_POST['aktiv-stelle']) and $_POST['aktiv-stelle']!='' ){ $terms = $_POST['aktiv-stelle']; wp_set_object_terms($post_id, $terms, 'stelle'); } } }
Can you please confirm that the solution I shared help you to resolve your issue. If so, please feel free to mark resolve this ticket.
Good morning Minesh,
I tested your solution and it works wonderfully.
Thank you very much for your help and patience.
Francesco
Glad to know that solution I shared help you to resolve your issue. You're welcome to mark resolve this ticket.
My issue is resolved now. Thank you!