Skip Navigation

[Resolved] Auto-fill Parent Taxonomy if Child is selected – Drop down list

This support ticket is created 2 years, 6 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 9 replies, has 2 voices.

Last updated by Luo Yang 2 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#2355655
parent-not-auto-select-dropdown-list.jpg

I'm trying to have the parent taxonomy auto selected when one child is.
I've read and tested the javascript code following this tread: https://toolset.com/forums/topic/select-the-parent-if-user-select-the-child-js/
It works well, but only on the "checkbox" mode. If I use the dropdown list mode, then the parent is not selected.
I would prefer to use the dropdown list mode, as I have more than 150 items in the list and it's easier to visualise this way.

#2356033

Hello,

The thread you mentioned above is for Toolset post forms, which works only in frontend, but the screenshot you provided above is for WordPress admin side, so those custom codes won't work, and there isn't such kind of built-in feature within Toolset plugins.

But in my opinion it is required to select the parent terms automatically, since when you query posts, and filter by taxonomy parent terms, WordPress will include posts which are assigned their child term automatically, see WP document:
https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
include_children (boolean) – Whether or not to include children for hierarchical taxonomies. Defaults to true.
So you don't need to enable parent terms by default.

#2356285

Hi Luo,
I realise my picture is a bit confusing.
- The left image is a screenshot of the frontend CRED form.
- The right image shows the result I see in admin / on the file once the form is submitted (only the child is selected).

As I said, when I use the checkbox mode in the form (frontend), the admin result is that the parent is also checked (backend). But if I use a dropdown list (frontend), only the child is selected, not the parent (backend).

Catherine 🙂

#2356759

As I mentioned above, it is not required to select those parent terms automatically, when you filter the result by parent terms, WordPress will include posts which are assigned their child term automatically:
https://toolset.com/forums/topic/auto-fill-parent-taxonomy-if-child-is-selected-drop-down-list/#post-2356033

If you still need assistance for it, since it is a custom codes issue, please provide a test site with the same problem, fill below private message box with login details, also point out the problem page URL and form URL, I need to test and debug it in a live website

#2356805

Hi Luo,
I think my problem is not clear.
I have a CRED form for users to publish a housing add. Filling the form up, they need to select the district.
My district taxonomy have up to 3 levels of data: cities (level 0) have districts (level 1) and sub-districts (level 2).

I want the post to auto-select levels 0 and 1 (Montréal / Rosemont), when a sub-district (level 2) is selected (Petite-Patrie), so all 3 levels are selected in the post data.

Using the following code (in my functions.php theme file), I was able to make the parent (levels 0 and 1) auto-selected, but it works only if I display the district taxonomy in checkbox mode.

If I display the district taxonomy in a dropdown list, then the parents (levels 0 and 1) do not auto-select and I get a post with only one level selected, the sub-district (level 3).

Here is the code (working for checkbox districts display) :
jQuery(document).ready(function($){
id = jQuery('[name="quartier-ili[]"]').attr("id");
$("input[name='quartier-ili[]']").each(function(index, input) {
$(this).on("change",function(){
var checkbox = $(this);
var is_checked = $(checkbox).is(':checked');
var parent = $(this).data("parent");
if(is_checked) {
$('input[value="'+parent+'"').prop('checked', true);
}else{
var count = $("input[name='quartier-ili[]']:checked").length - 1;
if(count==0){
$('input[value="'+parent+'"').prop('checked', false);
}
}
});
});
});

Is there a version of this code that would work with a dropdown districts display?

Catherine 🙂

#2356851

Since it is a select dropdown field, which can select only one choice, you can not select multiple choices in it, it is a limitation of HTML select field, so there isn't such kind of feature.

And there isn't such existed a version of this code that would work with a dropdown districts display, you might consider other workaround, for example:
Display it as multi-select field, then use custom JS codes to select the parent term.

More help:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-control-post-taxonomy'checkbox' | 'checkboxes' | 'date' | 'datepicker' | 'radios' | 'select' | 'textfield' | 'multi-select'

#2357187

When I try to display the taxonomy, I have:
- checkbox: [cred_field field='quartier' force_type='taxonomy' output='bootstrap' display='checkbox']
- select (1 choice only): [cred_field field='quartier' force_type='taxonomy' output='bootstrap' display='select' single_select='true']
- select (multiple choices): [cred_field field='quartier-ili' force_type='taxonomy' output='bootstrap' display='select']

I don't have "multi-select". I if I change it manually, it brings me back to the checkbox mode.

If I use the select mode with multiple choices, what would be the javascript updated code so the parent is automatically selected?

Catherine

#2358535

Since it is a custom codes issue, please provide a test site with the same problem, fill below private message box with login details, also point out the problem page URL and form URL, I need to test and debug it in a live website

Private message box enabled again

#2365581

Thanks for the details, I am checking it in your website, will update here if find anything

#2365617

I have add below JS codes into the post form:

jQuery(document).on('cred_form_ready', function() {
  jQuery( 'select[name="quartier-ili[]"]' ).change(function(e){
    var id = jQuery(this).attr("id");
    jQuery( '#' + id + ' option:selected' ).each(function(){
    	var parent_id = jQuery(this).attr('data-parent');
      	jQuery( '#' + id + ' option[value="' + parent_id + '"]' ).prop('selected', true);
    });
  }); 
   
});

When user checked one option of multiple selector, it will check it's one higher level term automatically, for your reference.