Skip Navigation

[Assigned] Filter “if field has value”

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 6 replies, has 1 voice.

Last updated by arnoldd 8 hours, 37 minutes ago.

Assisted by: Minesh.

Author
Posts
#2868246

I'm struggeling a bit. I have a view with a search filter. All good.
But i want to ad a filter option.

On a custom post type, companies kan place an action/discount.

I want to filter on that with only one checkbox. So, if they have an action this company will show when visitors select the checkbox "has action".

What is the way to do that?

#2868309

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share problem URL where you have your existing view and also send me admin access details and let me reivew your current setup and then I will guide you in the right direction or try to provide the possible solution.

*** 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 have set the next reply to private which means only you and I have access to it.

#2868325

Minesh
Supporter

Languages: English (English )

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

I would like to have bit more informatoin here.

When the view loads first time on the page load with the following link:
- hidden link

By default - what posts you want to display? Do we include posts where action value is set or there is no action set? or do you have any other requirement?

#2868327

Display all posts. Just as it is now. I only need one extra filter as explained.

#2868338

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

With you view I've added the checkbox filter as given under within "Search and pagination" section:
=> hidden link


<div class="form-group">
	<label for="wpv-wpcf-nieuwbouwactie-titel">[wpml-string context="wpv-views"]Nieuwbouwactie titel[/wpml-string]</label>
	[wpv-control-postmeta field="wpcf-nieuwbouwactie-titel" type="checkboxes" source="custom" placeholder="Nieuwbouwactie" url_param="wpv-wpcf-nieuwbouwactie-titel" values="1" display_values="Has Action"]
</div>

I've added the following code to "Custom Code" section offered by Toolset with the code snippet "toolset-custom-code":
=> hidden link

add_filter('wpv_filter_query', 'func_filter_posts_by_checkbox', 99, 3);  
function func_filter_posts_by_checkbox($query_args, $view_settings, $view_id) {
   
if($view_id == 1227 and (defined( 'DOING_AJAX') and DOING_AJAX) )  {
   
  
  		$is_checked = false;
        foreach($_POST['search']['dps_general'] as $k=>$v):
                if($v['value'] == '1'  and $v['name'] == 'wpv-wpcf-nieuwbouwactie-titel[]'){
                     $is_checked = true;
                }
                           
        endforeach;

         $meta_query = $query_args['meta_query'];
       $hook_meta_query = array();
        foreach($meta_query as $k=>$v):
            if($v['key']=='wpcf-nieuwbouwactie-titel' and $is_checked){
                   $found_index = $k; 
              	   
                   $hook_meta_query =  array('key'=>'wpcf-nieuwbouwactie-titel',
                                             'EXISTS');
                   break;

              }
  		endforeach;
  		if(!empty($hook_meta_query)){
          	$query_args['meta_query'][$found_index] = $hook_meta_query;
        }
         
     
   }   
return $query_args;
}
#2868344

Hey Minesh,

Thanks, but still some fault. If i check the checkbox on the filter i get 4 results. But there are only 2 results.

Only the cpt "Nieuwbouwspecialisten" whit a value/filled in textfield "Nieuwbouwactie titel" needs to show.

They have a yellow banner also in the grid with "Nieuwbouwactie"

#2868378

And the filter is now an "AND" filter with "Provincie" filter. This is after adding your custom code. It needs to be normal filter function.

If it's to difficult than we skip it.