Skip Navigation

[Resolved] View Query on Date

This support ticket is created 5 years, 9 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 5 years, 9 months ago.

Assisted by: Nigel.

Author
Posts
#1190332
download.png

I created a membership site.

I want to display users birthdays in the current month.

I've created a view and the result displays all birthdays.

That works fine.

When I add a query filter following the Date Filters instructions at https://toolset.com/documentation/user-guides/date-filters/ I get a "No items found" The filter is set to "number" that is equal to "THIS_YEAR".

Please help

#1190429

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Because of how date fields are stored, as UNIX timestamps which refer to a point in time, unless you write custom SQL queries it is not possible to filter the list of users in this way.

What is required is to manipulate the results after the database query has been run.

Ordinarily I would direct you to use conditional display, i.e. you View would return all users, and then in the output section you would only output those for which the birthdate (output to just show the month) matched today's date (output to just show the month).

This works fine, but is not so efficient on larger sites with many users. If you are creating a membership site, I guess you would want many users, so the alternative is to do something similar, but to use the wpv-filter_user_post_query hook to manipulate the list of users returned by the View before it outputs them.

I created some sample code that you can try (add a custom snippet at Toolset > Settings > Custom Code), where you will need to edit the View ID and the field name used for the birthday:

function tssupp_filter_birthdate($items, $query_args, $view_settings, $view_id) {

	if (in_array($view_id, array(208)) && !empty($items)) { // Edit View ID

		foreach ($items as $key => $item) {

			$birthdate = get_user_meta($item->ID, 'wpcf-birthdate', true); // Edit user meta key

			if ($birthdate != '') {

				$birthmonth = date('n', (int) $birthdate);
				$currentmonth = date('n');

				if ($birthmonth != $currentmonth) {

					$items[$key] = null;

				}
			} else {
				$items[$key] = null;
			}
		}
		$items = array_filter($items);

	}

	return $items;
}
add_filter('wpv_filter_user_post_query', 'tssupp_filter_birthdate', 101, 4);

Try the above and let me know how you get on.