Skip Navigation

[Resolved] Filter view by date greater than today OR blank but already using AND

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

Last updated by Waqar 5 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#1357357

Hi

I have a view but I am using and "AND" statement option for the filter as I am filtering by an ID field and the date being greater than today but I also want to show posts if the date isn't set, is blank.

Is there a way to do this? This is effectively ID=X AND date=blank OR date=greater than today.

However you can't use an AND and an OR statement in the filter as you know, so I am wandering if there's a way to simply set this when looking at the date?

Thanks.

#1357627

Hi Matthew,

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

To suggest the best way forward, I'll need to see how these fields and the existing filters are set up in the view.

Can you please share temporary admin login details, along with the name of the view in question?

Note: Your next reply will be private and though no changes will be made on your website, please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1358893

Hi Matthew,

Thank you for waiting, while I performed some research and testing around your requirements.

Before moving on to the possible solutions, one important point to remember is that the way custom field query works in WordPress, a "non-existent custom field" record is not the same as the blank or a "0" value.

What this means is that if you're planning to apply some filter to the custom field with key "wpcf-end-date", it is important that an empty or a 0 valued record exists with that key against all your posts. Any post which won't have any custom field record with that key will simply be ignored by that query.

First, a simpler approach. Since you're pulling/syncing data from a feed, you can explore if a rule can be set to assign a fixed end date from the far future for all the posts which don't have any actual end date. This way simple date filter with "End date value greater than or equal to TODAY" will do the job.

If setting a far future date is not possible, you'll need to create 3 views.

1. The first view will be set to get all posts where "Advertiser ID" is equal to the shortcode attribute "retailerid" and the End Date is equal to an empty constant.

Example screenshot: hidden link

2. The second view will be set to get all posts where "Advertiser ID" is equal to the shortcode attribute "retailerid" and the End Date is greater than or equal to TODAY.

Example screenshot: hidden link

Important note: Please make sure that both these views only return the IDs of the posts in an array format, which will then be called in a PHP function, for the third view

3. The third view will be set to show all posts without any filter but using the "wpv_filter_query" filter, it will be made to call in only posts, whose IDs are included in the output of the first and the second view.
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )

Example snippet:


add_filter( 'wpv_filter_query', 'show_combined_posts_fn', 1000 , 3 );
function show_combined_posts_fn( $view_args, $view_settings, $view_id ) {

	if ( ( !is_admin() && isset($view_settings['view_id'] ) ) && ( ($view_settings['view_id'] == XYZ) ) )
	{

		$IDs_from_view_1 = array("1", "2");
		$IDs_from_view_2 = array("3", "4", "5", "6");

		$view_args['post_in'] = array_merge($IDs_from_view_1, $IDs_from_view_2);   
		
	}

	return $view_args;
}

Please replace "XYZ" with the actual ID of the third view and the "$IDs_from_view_1" and "$IDs_from_view_2" will hold the IDs from the first and the second view, using the "render_view" function.
https://toolset.com/documentation/programmer-reference/views-api/#render_view

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

regards,
Waqar