Skip Navigation

[Resolved] 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 20 replies, has 1 voice.

Last updated by arnoldd 9 hours, 20 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.

#2868460

Minesh
Supporter

Languages: English (English )

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

I've adjusted the code as given under:

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('relation'=>'OR',
                                            
                     						array('relation'=>'AND',
                     								array('key'=>'wpcf-nieuwbouwactie-titel',
                                              				'compare'=>'EXISTS'),
                     								array('key'=>'wpcf-nieuwbouwactie-titel',
                                               				'value'=>'',
                                               				'compare'=>"!=")
                                            
                                            )
                                        );
                   break;

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

I see there are three posts available where action field value is setup and I can see now it returns those three posts when you filter with the checkbox filter "Nieuwbouwactie".

#2868463

Super! Thanks.
But there is only one problem now. The filter "Provincie" does not work as expected/before. It's needs to work just like the filter "Branche". If checked then it shows result. Now it seems to have an relation with "nieuwbouwactie". Should i make a taxonomy of "provincie" instead of a custom post to make it work correct?

#2868465

Minesh
Supporter

Languages: English (English )

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

On this page: hidden link

I checked the checkbox filter "Nieuwbouwactie" and it shows three results. Then I try to apply the "Provincie" filter and selected the option "Utrecht" and it shows me filtered result with one post.

How exactly "Provincie" filter should work?

#2868467

They need to work separate from each other. Just like like "branche" and "nieuwbouwactie" work separate from each other.
There is no direct relationship between them.

It worked oke before you applied the custom code.

#2868468

Minesh
Supporter

Languages: English (English )

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

I just disabled the custom code and on this page:
- hidden link

When I try to apply the "Provincie" filter and selected the option "Utrecht" and it shows me filtered result with only one post. That is also same result when custom code is active.

#2868469

Thats how it needs to work. But now the option "nieuwbouwactie" does not work. When you check this the result still needs to be one, because the result also had that option as wel. Just as if you check "zonwering". Same result : 1

If you activate the code, you get different behavior.

#2868470

Minesh
Supporter

Languages: English (English )

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

Well - I disable the code to demonstrate you.

If you can share video how exactly you want this to work that would be great as this is confusing.

I wonder how come user will know that this filter will be used separately and user do not click or apply the what filters in what condition.

Its better to have video explaining this and how exactly you want to work rather just describing here in text.

#2868475

Can you activate the filter? Then it's more easy to explain what goes wrong for me.