Skip Navigation

[Resolved] Show a "child" post, if it has the same custom field value ("date") as parent

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

Last updated by Lara 3 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#1603215

Tell us what you are trying to do?
My parent post has a "date" custom field. My child post field has a "first day" custom field and a "last day" custom field. I try to create a view, where all child posts are shown if ..

"first day child custom field" =/< "date parent custom field" =/< "last day child custom field"

The child post view should be inserted in a view, which lists all the parent posts. Is this possible?

Is there any documentation that you are following?
No. I couldn't find anything similar

Is there a similar example that we can see?
https://toolset.com/forums/topic/checking-for-weekdays-and-a-specific-dates-in-a-view/
Something like that, before the person, who asked the question changed the direction and developed something else.

What is the link to your site?

#1604015

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Lara,

Thank you for contacting us and I'd be happy to assist.

From your message, I understand that you'd like to show a child post related to a parent post, only if:

"first day child custom field value" is smaller than or equal to "date parent custom field value"
AND
"date parent custom field value" is smaller than or equal to "last day child custom field value"

If that is correct, you can first create a nested related post view, that lists all child posts:
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/

But wrap the output of the child post's view inside a conditional block, so that it is only shown when the condition is met:
https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/nesting-and-combining-conditions/

For example, suppose that in the child post view's loop, these are the shortcodes that work to show:


first day child custom field value -> [types field='first-day' output='raw'][/types]

last day child custom field value -> [types field='last-day' output='raw'][/types]

date parent custom field value -> [types field='parent-day' output='raw' item='@relationship-slug.parent'][/types]

Note: You'll replace field slugs "first-day", "last-day" & "parent-day" with the actual field slugs and "relationship-slug" with the actual post-relationship slug, used on your website.


[wpv-conditional if="( '[types field='first-day' output='raw'][/types]' <= '[types field='parent-day' output='raw' item='@relationship-slug.parent'][/types]' ) AND ( '[types field='parent-day' output='raw' item='@relationship-slug.parent'][/types]' <= '[types field='last-day' output='raw'][/types]' )"]

Show only when the condition is met

[/wpv-conditional]

As a result, the child view will bring in all related posts, but only the ones fulfilling the condition will be shown.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1604227

Hi Waqar,

I love your solution. Mhm, the only thing I am worried about is, that there are approximately between 400 and 600 child posts or more, so I am a little bit worried, that it will be very slow. This gives me a little bit a headache. That's why I hoped that the problem could be solved with a Query Filter. Is it impossible to solve it with a Query Filter or possbile, but very difficult?

Kind regards
Lara

#1604527

Hi Waqar,

I think I explained it badly. It's more like..

A view with Posts from Posttype A ("DAY") and in this view is a nested a other view, which contains posts from Posttype B ("ACTIVITY")

1) DAY | 28th April 2020
- ACTIVITY 1 (every Tuesday from 28th April 2020 until 28th June 2020)
- ACTIVITY 2 (only on the 28th April 2020)

2) DAY | 29th April 2020
- ACTIVITY 3 (every Wednesday from 29th April 2020 until 28th May 2020)
- ACTIVITY 4 (only on the 29th April 2020)

3) DAY | 30th April 2020
- ACTIVITY 5 (every Friday from 30th April 2020 until 20th June 2020)
- ACTIVITY 6 (only on the 30th April 2020)

4) DAY | 1st May 2020
- ACTIVITY 7 (every Saturday from 1st May 2020 until 28th July 2020)
- ACTIVITY 8 (only on the 1st May 2020)

I know that this can be achieved through a Many-two-Many-Relationship. But it means that I have to connect every activity to every day manually. For two months or more this is a lot of work. That's why I was looking for a workaround. And I came up with the idea to compare Posts from Posttype A with Posts from Posttype B like you described it here:

<quote>From your message, I understand that you'd like to show a post from posttype B only if:

"first day child (posttype B) custom field value" is smaller than or equal to "date parent (posttype A) custom field value"
AND
"date parent (posttype A) custom field value" is smaller than or equal to "last day child (posttype B) custom field value" </quote>

Not sure if this is possible...

Kind regars
Lara

#1605519

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Lara,

Thanks for writing back and for sharing these further details.

Your concerns about performance, when using the conditional comparison method make sense when a large number of posts are involved.

I'm not sure whether it was intentional or not, but the sample day and activity data that you've shared suggest that the activity's end date doesn't have to be in the equation at all.

"For an activity to show under a particular day, only its start date has to be the same as that day".
( ref: hidden link )

If my observation about your requirement is correct then you can create a parent post view that lists all "Day" posts.

And inside that view's loop, you can nest another post view to show the "Activity" posts.

You can pass the raw value of the date field of the "Day" post type in the shortcode attribute of the nested view and then include a query filter in the nested view for the start date custom field, that shows only those posts where this custom field's value is the same as the passed shortcode attribute value.
( ref: https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/ )

regards,
Waqar

#1614089

Hi Waqar,

many apologies for the delay. I have been unwell for a few days.

<quote>You can pass the raw value of the date field of the "Day" post type in the shortcode attribute of the nested view and then include a query filter in the nested view for the start date custom field, that shows only those posts where this custom field's value is the same as the passed shortcode attribute value.</quote>

This is really a great approach. I really like it. Thanks 🙂

Kind regards
Lara

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.