Skip Navigation

[Resolved] Modify view filter query with additional condition

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

Last updated by Waqar 2 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#2582277

Tell us what you are trying to do?
I want to be able to show a different post content each day, I want to add a condition that if the custom field (say display_order = x, x can be changed programmatically) then the view will display only that post that satisfies the value of display_order, this custom field is associated with a custom post type. How/Where/What code snippet is needed to accomplish this.

Is there any documentation that you are following?
I search in the forum but could not find a resolution.

Is there a similar example that we can see?
N/A
What is the link to your site?
N/A

#2582421

Nigel
Supporter

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

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

Hi there

Views are based on the underlying WordPress WP_Query class, and you can add additional query arguments via the wpv_filter_query filter (see https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query).

So you could use that hook to add a meta_query argument for your display_order field, whose value you set programmatically (e.g. based on the current day of the week).

#2582729

Thank you, Can you please point me to an example that uses this on an existing front-end query filter?

#2582875

Hi,

Thanks for writing back.

Here are the details of the setup that I used on my test website:

1. I added a single line custom field 'book-day', to my 'books' post type, and stored the weekdays in the different book posts, based on the days, I'd like to show them.
( e.g. Sunday, Monday, Tuesday, Wednesday, Thursday, etc )

2. Next, I used the following code snippet, to make the view only show the book posts, where the 'book-day', custom field value is the same as today's weekday:


add_filter( 'wpv_filter_query', 'filter_date_custom_fn', 1000 , 3 );
function filter_date_custom_fn( $query_args, $view_settings ) {
	if ( (!is_admin() && isset($view_settings['view_id'])) && ($view_settings['view_id'] == 12345) ) {
	$query_args['meta_query'] = array(
					'relation' => 'AND',
					array(
						'key'     => 'wpcf-book-day',
						'value'   => date("l"),
						'type'	  => 'CHAR',
						'compare' => '=',
					)
			);
	}
	return $query_args;
}

Note: You'll replace '12345' with the ID of the view where you'd like to apply this filtering.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

I hope this helps and here are some useful guides on the topic:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://developer.wordpress.org/reference/classes/wp_query/

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