Skip Navigation

[Resolved] Filter by date field on parent type

This support ticket is created 7 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+01:00)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by mikes-23 7 years, 9 months ago.

Assisted by: Nigel.

Author
Posts
#429280
q2.png
q1.png

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?

#429413

Nigel
Supporter

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

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

Hi Mike

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.

You need to add a conditional shortcode to only display anything according to your parent custom field, and you will target the parent by using id="$tour-dates" in the types shortcode for your date custom field (https://toolset.com/documentation/customizing-sites-using-php/functions/#date)

See the following documentation for adding your conditional shortcode:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
https://toolset.com/documentation/views-shortcodes/#wpv-conditional

Let me know if you have problems setting that up and I can help you further.

#429534

Hi Nigel,

I am guessing you mean to do something like:

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?

#429633

Nigel
Supporter

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

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

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...

#429754

Is there any way to check if the date value is greater than today with the shortcode?

I want to filter out the dates from the past. The example you gave checks if it has a value so it will include the past events.

Can I pass the parent date field as an attribute in my custom shortcode and then do a simple check with php?

#429768

I tried this:

[wpv-conditional if="( '[wpv_filter_future_date date="[types field='tour-date'  output='raw' id='$tour-events'][/types]"]' eq '1' )"]
          future
[/wpv-conditional]

The shortcode doesn't even fire when I try to pass attribute like this so something is breaking it.

#429791

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.