Skip Navigation

[Resolved] Dynamic Dropdown values added using wpt_field_options not working as filter

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

Problem:
How to display only parent taxonomy term using view's taxonomy dropdown filter

Solution:
You need to use taxonomy view and custom JS in order to remove child terms from your view's taxonomy filter.

You can find proposed solution with the following reply:
=> https://toolset.com/forums/topic/dynamic-dropdown-values-added-using-wpt_field_options-not-working-as-filter/page/2/#post-598295

Relevant Documentation:

This support ticket is created 7 years 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
- 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 16 replies, has 2 voices.

Last updated by marcB-6 7 years ago.

Assisted by: Minesh.

Author
Posts
#597683
Screenshot_2.png
Screenshot_3.png

Problem : Add checkbox filter for parent custom taxonomies.
I did research to achieve above goal but did not found anything to Add checkbox filter for parent custom taxonomies.

Solution : I have added custom dropdown field for each post type named as "Select parent category".
Now, to show parent custom taxonomies as dropdown options I have added below code.

add_filter('wpt_field_options', 'fill_select', 10, 3);

function fill_select($options, $title, $type) {

    if ($title == 'Select Parent Category') {
        $args = [
            'taxonomy' => 'location-type',
            'parent' => 0,
            'hide_empty' => false
        ];
        $terms = get_terms($args);
        $options[] = array(
            '#value' => 0,
            '#title' => "Select",
        );
        foreach ($terms as $term) {
            $options[] = array(
                '#value' => $term->name,
                '#title' => $term->name
            );
        }
    }
    return $options;
}

I have added filter using custom dropdown field "Select parent category".
But I am not able to see the values and checkboxes.

One thing I have noticed is, If i add dropdown option using toolset it is working fine.

Thanks

#597707

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - 'wpt_field_options' filter will not work in this case.

As I understand, you want to display only parent terms using view's taxonomy filter - correct?

#597714

Yes

#597717

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

what you need to do is - you need to add your taxonomy filter normal way but you need to add custom jQuery code to your view's filter JS box and delete the all options that are child.

Please check this related solution - this is for select box:
=> https://toolset.com/forums/topic/parametric-search-with-only-parent-taxonomy-on-pulldown-menu/#post-465858

#597933

Ok thanks, but I am not selecting parent taxonomy when I am creating post. I am selecting child taxonomy while adding post. Now I would like to add checkbox filter for parent taxonomy.

For example,
Restaurants ===> Parent
- Indian ===> Child
- Italian ===> Child
Watersports ===> Parent
- scubadiving ===> Child
- kitesurfing ===> Child

When I am adding post called "Taj Hotel" I am selecting "Indian" as custom taxonomy..Now I would like to add filter checkbox for "Restaurants","Watersports".

As I am not selecting parent taxonomy while creating post I will not get "Taj Hotel" when I check "Restaurants".

Please let me know How to do this

#597936

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

No - when you will add taxonomy filter normally to your views - to display all taxonomy terms, navigate to:
=> Custom Search Settings => Let me choose individual settings manually => set option "Always show all values for inputs"

So - it will display all taxonomy terms with your taxonomy dropdown, now, you should follow the way I suggested using jQuery to remove unwanted terms from your taxonomy filter drop-down.

Please check this related solution - this is for select box:
=> https://toolset.com/forums/topic/parametric-search-with-only-parent-taxonomy-on-pulldown-menu/#post-465858

#597937

Yes I have followed below steps
=> Custom Search Settings => Let me choose individual settings manually => set option "Always show all values for inputs"
Its working..Thanks for your support.

#597938

Thanks

#597939

Its working But I have around 150 taxonomies..So Do I need to add all in to an array as mentioned here : https://toolset.com/forums/topic/parametric-search-with-only-parent-taxonomy-on-pulldown-menu/#post-465858

This is not proper way to do. In future If I add 50 more then I need to add those as well..

Is there any other solution?

#597940

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - this is not normal situation as filter will work for selected taxonomy term.

You should try to use view's hook wpv_filter_query and check if "include_children" is set to 1.

For example:

add_filter( 'wpv_filter_query', 'show_parent_recoreds', 10, 3 );
function show_parent_recoreds( $query_args, $settings, $view_id ) {
        if($view_id == 9999){
              $query_args['tax_query'][0]['include_children'] = 1;
            
    }
    return $query_args;
}

Where:
-- Replace '9999' with your original view ID
-- Adjust the code as needed

Does this help?

#597941

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - there is no dynamic solution available.

You need to follow the way I already suggested here:
=> https://toolset.com/forums/topic/dynamic-dropdown-values-added-using-wpt_field_options-not-working-as-filter/#post-597939

#597948
Screenshot_4.png
Screenshot_5.png

I have put code given by you above.
As you see in attached screenshot, There are two filter added for two different taxonomies.
When I am doing

echo "<pre>";
    print_r($query_args);

I see only 1 taxonomy in tax_query that is location-theme.
I have another taxonomy as well which is location-types which I can not see when I am printing $query_args

#597951

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

OK - as I can see "include_children" is already set to 1, so no change needed. Now, regarding two taxonomy filter arguments, I can see that another taxonomy filter is setup using URL parameter. did you selected anything from your another taxonomy filter. If yes - it should show it, if no, it will not show it.

#597953

Please mark my next reply as private so I can provide you link and details.

#597954

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Please tell me what is your exact need.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.