Skip Navigation

[Resolved] Views query filter by post date OR custom field value

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to set up a Query Filter in a View that allows me to test if the post was published over 6 months ago, or if a custom field value is set. I cannot find how to use the "OR" relation between the post date and a custom field, only between multiple custom fields.

Solution: It's not possible to build this type of Query Filter in wp-admin, because the AND/OR feature only applies to custom field values, not post dates. You can use the wpv_filter_query or wpv_filter_query_post_process APIs to customize the Query Filter further, or you can use conditional HTML to hide results based on conditional evaluations.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

This support ticket is created 6 years, 4 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by ericaG 6 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#1071401

I'm trying to set up a query filter for a view using one of two criteria- I want to display posts that have either a value of 1 in Field A, OR a value of 2 in Field B. This doesn't seem to be possible! When I add an additional filter, I don't have the option of indicating AND or OR, it seems like by default the filter will select posts that have both a value of 1 in Field A and a value of 2 in Field B. Is there any way around this problem? I know that when I'm using fields conditionally in a view or content template, I have a choice of AND/OR when evaluating conditions. So it seems like I should have the same options when querying posts in the first place.

#1071526
Screen Shot 2018-08-02 at 12.05.02 PM.png

Hi, there's an option to choose "AND" or "OR" to link the custom field filters. Here's a screenshot showing the AND / OR option in the Query Filter panel. You can only choose one, so complex combinations that require different relationships between various fields are not possible in the GUI. That type of custom filter would require custom code using the wpv_filter_query API. If I've misunderstood the request, please let me know.

#1071539
Screen Shot 2018-08-02 at 12.18.07 PM (2).png

That is really bizarre... I don't have that option! See screenshot.

#1071589

Right, there's only one custom field filter in your screenshot so it won't appear until you add another custom field filter. The post date filter above doesn't count - the AND/OR is specifically for custom field filters only.

#1071602

Hmmm... so there is no way to choose AND/OR for any other filter besides custom fields? What I'm trying to accomplish is show a list of posts that either were published more than six months ago (the date query) OR posts that were published within the past six months, but have been "archived" (as indicated by a value in a custom field).

#1071632

No, unfortunately there is no way to set up that type of filter using the GUI in wp-admin. A combined publish date and custom field conditional will require custom PHP code using the wpv_filter_query or wpv_filter_query_post_process APIs.

We have documentation available for those APIs here: https://toolset.com/documentation/programmer-reference/views-filters/

You might be able to get around this restriction by using conditional HTML instead of Query Filters. For example, you could remove the two Query Filters and add a conditional around each item in the loop that checks to see if the post date is older than 6 months ago, OR if the post custom field value is set. This approach works best when your View is unpaginated, because posts hidden by the conditional will still count towards the number of posts in a page of results. In other words, if a page of results should show 10 items but 3 items in the current page are hidden by the conditional, only 7 items will appear in the results of that page.

#1073491

Thanks, this worked perfectly, I created a shortcode for the Unix timestamp 6 months ago and am evaluating that in my conditional statement.