Skip Navigation

[Resolved] Complex filter requirement

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

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 10 replies, has 2 voices.

Last updated by Minesh 4 years, 6 months ago.

Assigned support staff: Minesh.

Author
Posts
#869320

Hi

I'm a Drupal dev trying to transition to WordPress/Toolset so your help is much appreciated.

I have a Custom date field created using Types called "Available from". I have worked out how to modify the loop so that the field outputs the text 'Available now' if the date is equal to or less than today OR outputs the date ( like 'Available from 3/5/2018" ) if the date is in the future.

What I'd like to do is have an 'Available now' checkbox in the filters that limits the listings displayed to only those whose available from date is equal or less than today.

Any ideas ? PHP is fine.

Thanks

#871171

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - you can not add a checkbox for the custom date field as its not supported by default. Its a kind of custom requirement so you need to build filter using checkbox on your own.

For example - try to add following code to your view's filter box and add search button:

<input id="wpv_control_checkbox_wpcf-available-from" class="js-wpv-filter-trigger checkbox" name="wpv-available-from" value="1" type="checkbox"> <span> Available now </span>

Also add the Query filter for your custom field from Views query filter section and select your custom field "Available from" - so it should looks like something as follows:

Select items with field: available from is a string equal to URL_PARAM(wpv-available-from)	

Further, you need to use view's filter wpv_filter_query and you need to check if the checkbox is checked, add the filter and display the posts whose available from date is equal or less than today.

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

If you do not know how to do it, Please share access details and problem URL where I can see the checkbox you added with views and I'm happy to help you 🙂

#873072

Minesh

Thanks. I'm glad there is a way, but I'm struggling to follow your instructions. The site is currently just a test website on my local machine so difficult to share ( although I will get you access if we don't resolve this easily ).

I have added the following to the filter editor :

<div class="form-group">
    <label>[wpml-string context="wpv-views"]Availability[/wpml-string]</label>
    <input id="wpv_control_checkbox_wpcf-available-from" class="js-wpv-filter-trigger checkbox" name="wpv-available-from" value="1" type="checkbox"> <span> Only show dogs available now</span>[/wpv-filter-controls]
</div>

and my new checkbox shows up in the filters fine.

Your next instruction is "Also add the Query filter for your custom field from Views query filter section .......". Does this mean click on the 'New filter' button in the filter editor ? If so I have done that and added the following code :

<div class="form-group">
	<label>[wpml-string context="wpv-views"]Available[/wpml-string]</label>
	[wpv-control-postmeta field="wpcf-available" url_param="wpv-available-from"]
</div>

But I don't think this is correct ( it adds the from field to the filters ). Could you please try explaining again.

Many thanks.

#876232

Minesh
Supporter

Languages: English (English )

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

Your next instruction is "Also add the Query filter for your custom field from Views query filter section .......". Does this mean click on the 'New filter' button in the filter editor ?
=> No, Please check following Doc, how you can add "Query filter" to your view:
==> https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/

#879367

Minesh

Thanks that helped. The reason I couldn't understand before was that the 'Query filter' section of the form was not enabled in the 'Show on screen' settings.

I've now followed your instructions and added the following to my functions.php file :

//Return ???
add_filter( 'wpv_filter_query', 'sam_show_only_available_now' );
function sam_show_only_available_now( $query_args ) {
    global $current_user;
    //$types = (array) $query_args['post_type'];
    //if ( !is_admin() && in_array( 'company', $types ) ) {
        //$query_args['author'] = empty( $current_user->ID ) ? -1 : $current_user->ID;
    //}
    return $query_args;
}

I've used a debugger to investigate the $query_args and can see :

Array
(
    [post_type] => Array
        (
            [0] => dog
        )

    [paged] => 1
    [suppress_filters] => 
    [ignore_sticky_posts] => 1
    [posts_per_page] => -1
    [post__not_in] => Array
        (
            [0] => 38
        )

    [wpv_original_limit] => -1
    [wpv_original_offset] => 0
    [wpv_original_posts_per_page] => -1
    [post_status] => Array
        (
            [0] => publish
            [1] => private
        )

)

I am not sure how to modify the $query_args array to complete your instruction 'to check if the checkbox is checked, add the filter and display the posts whose available from date is equal or less than today.'.

Your further guidance is much appreciated.

#882117

Minesh
Supporter

Languages: English (English )

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

Well - is it possible for you to send me access details so I will be able to guide you in right direction.

Please send me problem URL and access details.

*** 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.

#887647

Minesh
Supporter

Languages: English (English )

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

Well - could you please share problem URL where I can see tthe checkbox filter you added?

#888367

Minesh

It's the only link in the menu hidden link.

Regards

Dave

#889149

Minesh
Supporter

Languages: English (English )

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

Ok - I have added search button to your filter box and I've added following code to your view's JS box:

jQuery(document).ready(function($){
  $.urlParam = function(name){
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (results==null){
       return null;
    }
    else{
       return decodeURI(results[1]) || 0;
    }
}
  param =  $.urlParam('wpv-available-from'); 
  if(param==1){
    $('#available_from').attr("checked","checked");
  }else{
      $('#available_from').removeAttr("checked");
  }
	 
  
  });

And I've adjusted the view filter code to your funtions.php file as given under:

add_filter( 'wpv_filter_query', 'sam_show_only_available_now',99,3);
function sam_show_only_available_now($query_args, $view_settings, $view_id) {
    global $current_user;
	
	if($view_id==37){
		
	
	
		$meta = $query_args['meta_query'];
		foreach($meta as $k=>$v):
			if($v['key'] == 'wpcf-available'){
				
					$filter_array = array('key' => 'wpcf-available',
					              		  'value' => time(),
									      'type' => 'numeric',
									      'compare' => '>='
									 );
				unset($query_args['meta_query'][$k]);					 
				break;
			}
		endforeach;
		$query_args['meta_query'][] =  $filter_array;
		
				
		
	}

    // WHAT DO I DO TO $query_args ?????
    
    return $query_args;
}

Could you please confirm its working on your end as well.

#889724

Minesh

Awesome. I just changed the comparison to less than or equal thus :

$filter_array = array('key' => 'wpcf-available',
					           'value' => time(),
                                                   'type' => 'numeric',
	                                           'compare' => '<='
			            );

as the checkbox was having the exact opposite effect to intended. Also wrapped the foreach in a check that $meta has a value to squash a PHP error on initial load without any filters :

$meta = $query_args['meta_query'];
    if($meta) {
		  foreach($meta as $k=>$v) {

I think I understand how this works now and this gives me the confidence that Views is extensible/alterable when more complex output is required. I will shelve plans to revert to Drupal 7 !

One last question ( probably ). Is there a reason why you inserted the submit button. Will it not work with AJAX update ?

Thanks

Dave

#891271

Minesh
Supporter

Languages: English (English )

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

One last question ( probably ). Is there a reason why you inserted the submit button. Will it not work with AJAX update ?
==> Well - the thing is that I do not have any native way to offer you solution using AJAX, as when you click on checkbox, the checkbox is not remain checked that you need to find a way if you want to use AJAX.