Skip Navigation

[Resolved] Conditional output comparing dates to today

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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 3 voices.

Last updated by Marcel 1 year, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#2583113

I've made a custom post type for advertisements. They have a start date and an end date, and should be shown only between those two dates.

I know I can use view filters to achieve this, but the advertisments are shown in between other posts (of another post type). If I use view filters for this, the other post types aren't shown anymore.

So I've used a conditional block, also since advertisments use a different template than other posts, and I would like to add a condition to show the advertisment only between the start and end date.

All topics I can find (like this one: https://toolset.com/forums/topic/conditional-dates/) use shortcodes instead of the block editor. Not sure how to use this.

#2583139

Nigel
Supporter

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

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

Screenshot 2023-03-29 at 15.22.50.png

Hi there

Before we can help, I think we need to be clear about where and how you are displaying these ads.

You want to place the ads between other posts.

Where should the ads come from?

A View queries posts and iterates over them.

Imagine we have a query iterating over and outputting these posts:

my-post-1
my-post-2
my-post-3
my-post-4
my-post-5

etc.

There are no ad posts there to filter, or to conditionally check.

If you are talking about a single ad (which changes according to the date) injected somewhere in the output of the posts, you could do that by adding a new View which queries the ad posts and which includes a query filter for the date so that only the intended ad is shown.

So you end up with something like:

my-post-1
my-post-2
my-post-3
ad-for-now
my-post-4
my-post-5

The output of the main View (for my-posts) would be set so that on the 3rd iteration of the loop as well as the normal content it would also output an Ad View (with filters for the date).

If that's the kind of thing you had in mind, note that nested Views are only supported in the legacy editor, although you can insert the second View (which should be made in the legacy editor) into the output of the first View inside a shortcode block.

If using the block editor, the way you would output something only for the 3rd loop iteration would be with a conditional block, see my screenshot for an example.

#2583179

Hi Nigel,

I've set this up a bit different. Advertisements is a seperate custom post type. I've configured the view to show both normal post and advertisements. I've used conditional blocks in the view so I can use a dedicated template for advertisements. This all works fine.

The only thing I need is a way to compare a custom date field to todays date in the conditional block 🙂

Although it would be even better if I could set that every 3th post or so should be an advertisement...

See this video: hidden link

#2583369
date-contion.jpg

Hello,

You can setup the condition as:
compare with "Date function"-> Today

See my screenshot date-contion.jpg

#2583563

My issue is resolved now. Thank you!