Skip Navigation

[Resolved] Can we use a same filter (a custom field) two times in a view.?

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

Last updated by andrewD-10 5 years ago.

Assisted by: Waqar.

Author
Posts
#1383709

Tell us what you are trying to do?
Hi team toolset!
We are developing a membership website for our customers which will contain a listing of construction projects. We are using toolset to create custom post types, custom fields and views.

i have 2-3 views as tables in my scenario, in one of my table i want sorting of bid date (a custom field of type:date) starts from most recent date in past (means yesterday in generic) to the earliest date in last as a default view of my table and i have achieved it using hidden link query filter and got result default state in desired sorted pattern hidden link , but problem comes when a user apply the filter on front end then it do not work may be because we have pre-defined it already.

1- i'm in need to work this filter again so users can filter and start seeing post from any particular below the dates (i:e, if a user wants to start seeing older then 6 months posts. i need that filter working in anyway. if there's no way out then we can discuss second plan to do that.
2- in second option if we set filter hidden link and setup it like whenever that page is opened then this filter is applied for each present date automatic hidden link and result is sorted by default, after whch user can update the value to filter according to their need. is it possible ?
3- is there any way that if i can use one a same filter two times (in a custom filed) in one view. like if i create two boxes in one page view (one on left and second on right) and want to execute on each time independently? is it possible.
Is there any documentation that you are following?

Is there a similar example that we can see?
No

What is the link to your site?
hidden link
it has access restrictions but we dont mind if you want to see inside code to know how it working inside.

Your help will be appreciated.

#1383847

Hi Andrew,

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

Your observation is correct and it is not possible to include more than one query filter in the view's settings, for the same custom field.

For this kind of query manipulation, you can use "wpv_filter_query" filter:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

To start with, you can keep the dynamic query filter in the view's settings, so that the front-end field can be used to filter the bid date by visitors.

As for the static filter that should apply on page load, by default, in absence of front-end field's URL parameter, you can create a custom function and attach it to the "wpv_filter_query" filter.

You'll find a good example of a code snippet very similar to your requirement in this reply:
https://toolset.com/forums/topic/how-to-use-wpv_filter_query/#post-1205374

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1384511

Hi Waqar,
Thanks for your assistance and to make sure that our problem can be solved. I went through both links and it seems to be helpful.
Can you help and write custom function to attach wpv_filter_query as a static filter to that past project table. i'm sure you understand my need that filter should be set to current date (whatever today date will be) to the lower dates in descending order. Same way for filter hidden link in order hidden link.
Please compose it in a way that it do not interact with any other table or do not disturb them.
Im sure this function will be added in the theme functions.php?
Your help will be really appreciated.
Regards

#1385567

Hi,

Thanks for writing back.

Although 1-1 custom code assistance is beyond the scope of support that we can provide, we do our best to guide in the right direction whenever possible.

To apply a fixed/static "bid date" query filter, when a user hasn't selected a value from the search form, you can update the code snippet from Luo's reply like this:


function filter_bid_date_default($view_args, $view_settings, $view_id) {

	if (in_array($view_id, array(1234))) {
		// slug of the bid date field
		$bid_field_slug = "wpcf-bid-date";

		// check for the existing meta query filters
		if(!empty($view_args['meta_query'])) {
			foreach ($view_args['meta_query'] as $meta_query_arr ) {
				if( (!empty($meta_query_arr['key'])) && (isset($meta_query_arr['key'])) ) {
					$available_keys[] = $meta_query_arr['key'];
				}
			}
		}
		else {
			$available_keys[] = '';
		}

		// if no user filter for the bid date exists add a fixed one
		if (!in_array($bid_field_slug, $available_keys)) {
			$view_args['meta_query'][] = array(
				'key' => $bid_field_slug,
				'value' => current_time('timestamp'),
				'type' => 'CHAR',
				'compare' => '<='
			);
		}
	}

	return $view_args;
}
add_filter('wpv_filter_query', 'filter_bid_date_default', 99, 3);

The above code snippet can be included in the active theme's "functions.php" file and please replace "1234" with the actual view's ID and "bid-date" with the actual slug of your field.

For more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1388465

Your help is appreciated Waqar.
This resolved my problem very well.

Thanks for the best assistance.

#1388467

My issue is resolved now. Thank you!