Skip Navigation

[Resolved] I need to customize a View filter

This support ticket is created 4 years, 7 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 19 replies, has 2 voices.

Last updated by francescoG-3 4 years, 6 months ago.

Assisted by: Beda.

Author
Posts
#1345219

Tell us what you are trying to do? I'm creating events site. For every event, I have created two datetime custom fields. The first datetime custom field is a repeatable field and contains the start datetime values of the event. The second datetime custom field NOT is a repeatable filed and contains the end datetime value of the event.
Now, I need to create a View filter where today is between the start datetime and the end datetime.
What should I do to do that?

Is there any documentation that you are following? I don't know.

Is there a similar example that we can see? I don't know.

What is the link to your site? hidden link

#1345259

To filter by the fields of a Repeatable Field Group you need to create a View, that queries that post type (Repeatable fields).

Then you can add Custom Filters, but note that currently you can (with the GUI) only add Query Filters, not Front End Filters.
That is not included in the GUI currently and there is a internal task for the Developers to add that.

So officially, you cannot yet search in the front end thru fields of a Repeatable Fields Group.
You can only query filter them, this means, the filter is not manipulated in the front end but only the backend.

I could show you a working hand-crafted Front End filter, but then, it would be unwise to apply that for a project that should be longstanding, likely, because Toolset should add these feature in future and they may then be different than what I suggest you.
In any case, you could do this (but remember, this is not yet officially possible in the GUI):

1. Create RFG (Repeatable Field Groups) for some posts and a View querying (all) of those RFG.
2. In the View Query Filter setup all filters for the Fields of the RFG
3. Then in the Front end Search (Custom Filter HTML editor) insert manually the ShortCodes and values for the front end filter.
==> You need to get the slug of each Field in your RFG that you used in the Query filter first.
[php]
<div class="form-group">
<label for="wpv-YOUR-FIELD-SLUG">[wpml-string context="wpv-views"]YOUR-FIELD-NAME[/wpml-string]</label>
[wpv-control-postmeta field="YOUR-FIELD-SLUG" type="select" url_param="wpv-YOUR-FIELD-SLUG"]
</div>
(Make sure that url_param actually matches what you used in the Query Filter.)

This will produce a front end search thru the fields of the RFG and it will work when used, meaning it will display the expected results (RFG's where the fields have the value as queried).

But that does not yet solve the issue of querying a data field against TODAY().

For this, your Query Filter that addresses the Start Dates should be checking if those fields are a NUMBER (lower than) the custom field where you store the date.
This will return all the date fields posts (RFG) where the start date is lower than today.

However, you cannot query by a field that is not part of the RFG, in a View that queries an RFG.
This is once more, a feature that is missing in Toolset currently.
I added your voice to that task as well.

Unfortunately currently the goal you try to achieve is only possible if you use 2 different Views, where one queries the post type where the single field is shown, and the second queries the RFG that belongs to those posts.
But there, you could not add a front end filter, in the second view, because it would repeat for each post of the parent view.

You could only add query filters that allow to narrow the RFG with a hardcoded query, so to say, not editable by the user in the front end.

May I ask what the goal of several start dates is?
Maybe I can suggest a more comfortable solution to use with Toolset.

#1345267
View-query-filter-today.png

Hi Beda, thank you for your reply.

So, I need only to set the query filter of the View.
For the repeatable field, I can take only the first value using index="0".

So this, because I want to create a section to display the Today active events. Where TODAY is between Start Datetime Field and End Datetime Field.
Start Datetime (index 0) <= Today <= End Datetime

If I set two different query filters where Start Datetime = Today (but the Start Datetime field is a repeatable field and I cannot select the index 0) and End Datetime >= Today, the View loop doesn't return any values...

I think the issue is because the Start Datetime field is a repeatable field, and so it is an Array. I think this because I have set a View to display the posts list using only the End Datetime >= Today query filter and the loop display the list with posts that have the end >= TODAY.
But it should be more correct to use the Start Datetime (index 0) > TODAY query filter. And, what should I do to set the index 0 into the query filter datetime field?

See the attached image to see what I need to do. (P.S. Data e ora inizio = Start Datetime Field)

So, if it is not possible for now, I put it in stand by and I work on another section.

But, I thank you again for your reply, because you have indicated me a solution to set the front-end search filter and, because I will do that, now I know what I should do when I will do that.

#1345295

What you show in the screenshot is not possible, but you can compare as mentioned the Date Field (start or end or both) to TODAY as numbers.
The other way around it's not possible but it's the same outcome, you simply set the start date fields to be greater or lower than TODAY as you want by number and the end date field greater than TODAY so you get the later opening.

But I explained this is possible only on the same post type, so you cannot do this in one View where you query RFG or posts our both, but only in 2 views, one query fro the RFG the other for the Post, and hence the filter is not possible like this.

I would actually suggest removing the Start date from the RFG if somehow possible and make it a field of the very event post, not a repeatable field group, so you can query between start and end in the same view. I am not sure that will help you, however it's the only possible approach right now.

You could apply a Query filter in the RFG field for the repeater Date, and apply a custom filter to the query filter passing the second date as max, you could use https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query for that.
You'd need to pass a new query argument to the View programmatically, getting all "parent" posts and compare that date field in the code to the TODAY value, then update the resulting RFG's belonging to those posts.
That is too complex, it is also an expensive query, that is not entirely justified to get (even if repeating) events.

Rather I would setup many event posts here, each with its own start and end date, because if an event has a different start date, technically speaking it's another event.
The same event can never start at 2 different moments, so to speak, unless it has an open start, and then I'd again use a "start-start-slot date" and an "end-start-slot date" field, instead of repeater fields.

#1345315

So, I have set the start date as a repeatable field because the post could be related to a festival that has a duration more than one day, for example one week, with an event per day.

So, I think this could be the fastest way to build the event page.

I know that I can create a CPT for the Festivals, and put it in relationship with the event posts, where the author can create the festival page, and after can create the single events pages and put them in relationship with the festival page.

I don't know if it is more complex to manage for the users that will register them on the site to manage their events...

Do you think that is the best way instead of using start date field as a repeatable field?
And do you think that is more complex for me to build the frontend Form to create the Festival page and after the Events Pages?

#1345357
View-query-filter-today-2.png

I think I have solved using the query filter in the attached image but I don't know if the FUTURE_DAY is set properly, I think that FUTURE_DAY return value is Tomorrow. Is it this?

#1346599

Hi Beda,
I have changed the start datetime field from repeatable to normal datetime field.

In the module where I want to display the today events, I have set the query filter as "start datetime field = TODAY".
With this query filter I have some display problems... So, it display only the events that have the start time >= of now, but I need to display all events that have the start date set to today without calculating the time...

Also, I have created another module where I want to display only the future events, without displaying the today events... to do that I have set the query filter as "start datetime field > TODAY" but it also display the events that have the start date = today and start time > now...

This is a problem to do what I need... Is there a solution to do what I need?

#1346893

TODAY() will retrieve UTC Timestamp of today's midnight.
You can read more about this here: https://toolset.com/documentation/user-guides/date-filters/

So you will get all results that are greater than that midnight value - but in UTC.

Does that match what you see? You might then need to either adjust the start/end dates considering them to be UTC or adjust the filters by some hours, but the first is definitely better to do.

#1346981

A little suggestion for the developers to take the user timezone and using it into the date filters.
So, in the Toolset settings them can add a custom field related to timezone (for example $sitetimezone), and the toolset user can select his own timezone, or the date filter can take the timezone set into the WordPress general settings, and the TODAY() can be calculated using the following function code that I have used like as a shortcode, where 'Europe/Rome' could be the tooset settings custom field '$sitetimezone':

function wp_get_today() {
    $userTimezone = new DateTimeZone('Europe/Rome');
    $date = new DateTime('now',$userTimezone);
    return strtotime($date->format('Y/m/d'));
} 
add_shortcode( 'today', 'wp_get_today' );

or

function wp_get_today() {
    $userTimezone = new DateTimeZone('Europe/Rome');
    $date = date("d-m-Y",$userTimezone);
    return strtotime($date);
} 
add_shortcode( 'today', 'wp_get_today' );

Now, return to my question.
So, I think that the View to display today events have some problems to display the today events, something went wrong when the View load the loop with query filter...
Well, I have setted two events that start today, and I have adjusted the query filter with:
Start datetime field (as number) <= Today
End datetime field (as number) >= Today

But now the View display only "No items found".

And the future events section that has the query filter set to 'Start datetime field (as number) >= Future_Day(1)' now works very fine and show me only the future events.

To test correct post settings, I have created a new View to display the related events section: here the query filter is set to 'Start datetime field (as number) >= Today' and it also display the Today events...

What is wrong?
It need to clone the Today events View into a new View?

#1347085

You can submit a feature request for this here https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/.
The Product management will evaluate the request and eventually ask the Developers to implement it, however for the data issue as mentioned, there was already a discussion in past that resulted in keeping the UTC - it may be looked again at it, however there no tight solutions for this. Maybe the Developers can consider a solution like yours, however it would be a Feature to be requested.

Can you grant me access to the view in question, and the posts/field, so I can have a closer look at the filter?
Remember that it considers UTC times.

I might be able to find a solution for it while playing with the Views settings and having your post data as example to work with.
Best would be a staging site, if there is no such, a duplicate could do, unless you do not mind alterations to the live site.

I enabled a private form for either to be submitted safely.

#1347245

/wp-login leads to a 404 on that website, but I was able to get in on the classic admin url.

The View mentioned cannot query Fields of a Repeatable Field Group.
This because it queries Posts, and Views needs to query the post type it further restricts with a Query Filter.
You can not query another post type's fields when listing a certain type, like here.

So the View output of hidden link will always be Posts.
And you can only query that View by Fields that are added directly to the Post, not in Repeatable Field Groups.
Data e ora inizio is in a Repeatable field group and should hence not be used here, otherwise things will break.

There is no workaround to that, and a feature request to add it is filed (I added your voice to it).

If you want to display Posts, where their RFG (Repeatable Field Groups) fields have a certain value, then you would need to create a View that queries the RFG post type (Gruppo dichiarazioni, as I see).
There, you can add query filters for the fields content in the RFG itself, BUT not by fields from the parent post.
That would not block us, you could create a Parent View, to return all Posts restricted by the end date, which is always the same, and in this view insert the RFG View that is restricted by start date.

That should allow you to put Parent Post Data information in the RFG View's loop, so you can show the actual post (event) data on the front end.

I have made an example view here (Parent):
hidden link

Made a child view to query the RFG and it's start date.
hidden link

I wanted to query that by the Date Field in the Repeatable Field group, that is when I saw, you have no date field in the RFG.
So, the entire task of filtering RFG fields is not what you want to achieve?

Can you specify what the goal is, and which fields you want to compare?
The current fields are not in RFGs, so they do not require any special treatment.

They are not even repeating, as single fields.

What do I miss in the setup?

#1347279

Data e ora inizio is NOT in a Repeatable field group (RFG).
So, 'Data e ora inizio' was a repeatable field, that allow multiple values, but yesterday I have set it to allow only one value.

The RFG post type (Gruppo dichiarazioni) don't contain any datetime field. Look into this url to see how the custom fields are made hidden link

#1347929

I unterstand, I apologize the misunderstanding, then the whole situation is different.

You can easily query thru Repeating Fields, NOT thru Repeatable Field Groups.

For that, query the Post Type with the Field, and set the Query filter to a number not/like the date field.
No matter if that is repeatable or not, it will return ALL matching results (means, at least one of the repeating fields must match).

So now I understand it is a single field, and hence, you can set the filter for that field in the view that queries that post type.

No posts are shown because the filter cannot show any posts, since there are none in that filtered area.

See, the post you used hidden link is not a "senza categoria", so it will not appear in the results.
In fact as soon you remove the additional filter (category) things work.
(I had to edit the posts fine date, to today, so it can appear in the filters results, as it was set to yesterday).

I change everything back to as it was now, you can however set a post to expire (fine) today, maximally 23:59 minimally 00:00, and that post will appear in the view as long it has any category but the "no category".

#1348835

Thank you, now it works fine.
So, I have created a new CPT for the Festivals that contains multiple events, I have tested it creating this Festival page hidden link and with Relationship, I have created and related the events to the festival.

In the Today section, now I can see the today events, and in the Future section, I can see future events.

In the Festival page, under the map, I have added a section where are displayed all related events (but it needs more css customizations).
For now, it is all ok!

#1348847

Great - then we can close here.
You are always welcome to open a new ticket requesting assistance in our forum.

Thank you for using Toolset!

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