How do I filter by a custom date field on a parent post type?
I have a parent post type "tour-dates" and the child type "artist-event-app". Tour-dates has a date field called "tour-date".
I am only interested in showing events that are in the future and not in the past. On my view that shows all tour-dates this works fine using the number is greater than query filter.
However, on my view that lists artist-event-app and also outputs the tour dates, I cannot filter out the past dates in the same way. If I add the same query filter then no results at all are shown.
So how do I filter child posts based on the custom date field on the parent?
Filtering by custom field works on the thing that is being queried.
So when you are querying your parent post "tour-dates" it is meaningful to add a condition for which tour-dates are returned (by testing the custom field of each post for the date).
If you query the child post which doesn't have the custom field then you can't test that custom field, it is not possible unless you start writing your own SQL database queries.
To produce a list of artist-event-app's whose parents have future event dates you need to loop across all of your artist-event-app's but only output anything if the parent condition is met.
So make a view for your child post which returns them all and modify the loop output.
if (!function_exists( 'filter_event_date_future')){
function filter_event_date_future($var)
{
global $post;
$future = CheckIfFuture($post);
return $future;
}
add_shortcode( 'wpv_filter_future_date', 'filter_event_date_future');
}
Then in view wrap everything in:
[wpv-conditional if="( '[wpv_filter_future_date]' eq '1' )"]
do output
[/wpv-conditional]
I have a couple of questions about this approach:
1. For my CheckIfFuture() method, how do I get at the posts parent value? Do you expose any API for traversing object relationships and property values?
2. How do I stop this interfering with pagination? From what I can see this would be filtering the results after they have been paginated so it would lead to unequal pages and eventaully as time progresses several blank pages. You mentioned I could write a custom query. How would I go about creating that query and linking it to a view. Do you have any API for this?
I wasn't thinking of anything that required custom coding.
Here is the loop output for my child post view. In my case I just have a text field on the parent and I'm testing for whether it is empty or not and only displaying my child posts if that field on the parent is set. You will want a test for your dates, but you can see the principle. Take careful note of the single and double quotes used to embed the types shortcode inside the views conditional shortcode.
<wpv-loop>
[wpv-conditional if="('[types field="parent-field" id="$parent-post"]' ne '' )"]
<h3>My matching child post title is: [wpv-post-title]</h3>
<p>The parent post for this is: [wpv-post-title id="$parent-post"]</p>
<p>The test field on the parent is: [types field="parent-field" id="$parent-post"]</p>
[/wpv-conditional]
</wpv-loop>
Now you have identified the main problem with this technique: pagination.
Unfortunately there is not much to be done about that. You might not notice if you are using infinite scroll, for example, but the root of the problem is that we are testing the results after they have been returned.
Views is basically a user-friendly wrapper—with a few bells and whistles—for the native WP_Query. For things that are missing from Views, or require an extra degree of customisation, you can hook into manipulate the arguments that Views constructs to pass to WP_Query using the Views API: https://toolset.com/documentation/user-guides/views-filters/.
The problem is that it is not straightforward to do what you want even if you bypass Views and directly construct a new WP_Query yourself, which is why I said you would need to construct your own SQL query...
If your parent-child relationship were reversed then you wouldn't have the same problem.
It is possible to add a query filter to see if posts belong to a parent because of the way Types stores this relationship: as a custom field "_wpcf_belongs_parent-slug_id" in the wp_postmeta table. So posts know who their parents are but don't know who their children are...
I forgot to open SSH tunnel so my breakpoint wasn't hitting. I opened it and it is passing the date to the shortcode fine. I can filter it like this but I think ultimately this will degrade performance over time as more and more past events accrue. I'll have to try and work out how to do this with a proper query.
I'll close this ticket. Thanks for your help
This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.