Skip Navigation

[Resolved] Removing the default value of a select and set the first element as the default

This support ticket is created 3 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
- 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/Karachi (GMT+05:00)

This topic contains 8 replies, has 2 voices.

Last updated by nelsonT-2 3 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#1975073

Hi,

I want to filter some data based on a custom taxonomy. When I create the filter, I can use a select to filter them.

The problem is that I would like to remove the first "Show all" default element (I.e. just leaving it empty won't do) and use the first element from the select as the default value.

I.e. I have "Staff", "Boad of Directors", "State Affiliates", etc as categories. When I load the page, I would like "Staff" to be already selected and of course the view filtered by it.

I know that I can use a url similar to "url/?wpv-team-and-supporters-category=staff", but first it will not remove the first empty value (I could do it through jQuery, but I guess you have a better method through a php function) and I would like that by default I don't have to type a url setting (I.e. just "url/who-we-are/" so that I do have to update all the links or allow a user to remove the setting in the url and show everything. In other words, I'd like a function or a condition that says that if wpv-team-and-supporters-category is empty, then show "staff"... but since I can put just one filter per element/taxonomy, I can't really do that...

So, if there is a way in the View interface (I'm using the classic view, since I use Elementor), that's great! If not, is there a function that I could create to achieve this?

Thank you,
Nelson

#1975339

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Nelson,

Thank you for contacting us and I'd be happy to assist.

To pre-filter the view's results with respect to taxonomy term(s), you can use the "wpv_filter_query" filter:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

You'll find an example snippet for a similar requirement in this reply:
https://toolset.com/forums/topic/pre-filtering-a-view-using-custom-taxonomy/#post-1964079

regards,
Waqar

#1976045

Thanks a lot. All is working now and I added some jQuery code to hide the first empty element in the drop-down.

#1976221

Now that I've added content for another category, I found a but in that code...

It works well in the sense that if no category is specified (I.e. if the url doesn't have any parameters) it shows the default one that I want (Staff). BUT, the problem is that none of the other categories now appear in the drop-down... I.e. I just have the default "Show all" (that is blank for me and that I hide via jQuery, but if I remove my jQuery code it appears) and Staff (first element in the drop-down, which is also the default I set), but none of the other taxonomy's categories appear.

What I want it the Staff to be visible and selected by default, but the option to select something else in the drop-down (and filtered through AJAX to prevent reload).

Here's the current code:
add_filter( 'wpv_filter_query', 'wpv_filter_query_func', 1000 , 3 );
function wpv_filter_query_func( $query_args, $view_settings ) {
// skip if blocks edit screen
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return $query_args;
}

// process if specific view
if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 17255) ) {

$found_index = 'no';

$target_tax = "team-and-supporters-category";

$target_terms = array('0' => 631);

// check if filter for the target taxonomy exists
for ($i=0; $i < (count($query_args['tax_query']) - 1) ; $i++) {
if ( $query_args['tax_query'][$i]['taxonomy'] == $target_tax) {
$found_index = 'yes';
}
}
// if filter for the target taxonomy doesn't exist, add a custom one
if( $found_index == 'no') {
$query_args['tax_query'][] = array(
'taxonomy' => $target_tax,
'field' => 'id',
'terms' => $target_terms,
'operator' => 'IN',
'include_children' => 1
);
}
}
return $query_args;
}

I also noticed that if I had the other categories in the array:
$target_terms = array('0' => 631, '1' => 633, '2' => 632, '3' => 634, '4' => 635);

Then it does show all of them in the select... but also doesn't really filter, as it shows everything... So, bottom line, if I select just one, just that one is also in the drop-down, and if I select many, there no filter by default (I.e. same as Show All)... Since I only have 5 categories, I guess I could just create a select manually... but I would like to avoid that to make it dynamic in case the client adds or removes one category.

Thank you.

#1976223

Now that I've added content for another category, I found a but in that code...

It works well in the sense that if no category is specified (I.e. if the url doesn't have any parameters) it shows the default one that I want (Staff). BUT, the problem is that none of the other categories now appear in the drop-down... I.e. I just have the default "Show all" (that is blank for me and that I hide via jQuery, but if I remove my jQuery code it appears) and Staff (first element in the drop-down, which is also the default I set), but none of the other taxonomy's categories appear.

What I want it the Staff to be visible and selected by default, but the option to select something else in the drop-down (and filtered through AJAX to prevent reload).

Here's the current code:
add_filter( 'wpv_filter_query', 'wpv_filter_query_func', 1000 , 3 );
function wpv_filter_query_func( $query_args, $view_settings ) {
// skip if blocks edit screen
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return $query_args;
}

// process if specific view
if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 17255) ) {

$found_index = 'no';

$target_tax = "team-and-supporters-category";

$target_terms = array('0' => 631);

// check if filter for the target taxonomy exists
for ($i=0; $i < (count($query_args['tax_query']) - 1) ; $i++) {
if ( $query_args['tax_query'][$i]['taxonomy'] == $target_tax) {
$found_index = 'yes';
}
}
// if filter for the target taxonomy doesn't exist, add a custom one
if( $found_index == 'no') {
$query_args['tax_query'][] = array(
'taxonomy' => $target_tax,
'field' => 'id',
'terms' => $target_terms,
'operator' => 'IN',
'include_children' => 1
);
}
}
return $query_args;
}

I also noticed that if I had the other categories in the array:
$target_terms = array('0' => 631, '1' => 633, '2' => 632, '3' => 634, '4' => 635);

Then it does show all of them in the select... but also doesn't really filter, as it shows everything... So, bottom line, if I select just one, just that one is also in the drop-down, and if I select many, there no filter by default (I.e. same as Show All)... Since I only have 5 categories, I guess I could just create a select manually... but I would like to avoid that to make it dynamic in case the client adds or removes one category.

Thank you.

#1976247

FYI, I just did some tests with a manual select and it does filter, but the selected element is not selected in the select... So, it looks like I would need to create another code to detect the option in the URL and add a "selected" attribute manually in the option of the select... Doable, but not the best solution...

#1976277

Ok, found the issue... Needed to manually change the Custom Search Settings and set it to "Always show all values for inputs"... I was searching for this, but didn't remember where it was...

Thank you.

#1978085

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for the update and glad that you've found the "Always show all values for inputs" option.

You're welcome to mark this ticket as resolved and start a new one for each new question or concern.

#1980183

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.