Skip Navigation

[Resolved] Date Query Filter set by Custom Field

This support ticket is created 6 years, 10 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)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by ioannisM-2 6 years, 10 months ago.

Assisted by: Beda.

Author
Posts
#529152
Date Query Filter.png

Hello,

I have created the post type provider. The provider post type has the field wpcf-my-date-and-time. What I am trying to achieve is to show in a view content that it's published date is equal or after the date and time of wpcf-my-date-and-time of the provider post type. How could I achieve that?

Thank you!

#529173

The best to do here is a HTML condition like this:

[wpv-conditional if="( '[types field='post-date' style='text' format='U'][/types]' gt '[wpv-post-date format='U']' )"]
  Display what you need
[/wpv-conditional]

It's elaborated here:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

I used a Date Field made with Types, and use the Timestamp format in both, so we can accurately compare the values including seconds.

My code sample above checks if the Custom Field date is Bigger (later in future) than the Post Published Date, if so, it displays my content.
This can be adapted with The Conditional GUI.

#529234

Dear Beda,

Thank you for your response but I think that that may not work in my case. Let's see the steps one by one and explain:

1) I create the post type Provider
2) I create the post field for the provider wpcf-my-date-and-time
3) I create a view with Books .The books have no relationship with providers
4) I add in the content template of the Books the code

[wpv-conditional if="( '[types field='wpcf-my-date-and-time' style='text' format='U'][/types]' gt '[wpv-post-date format='U']' )"]
  Books Title
Book's Featured Image
[/wpv-conditional]


[wpv-conditional if="( '[types field='wpcf-my-date-and-time' style='text' format='U'][/types]' lt '[wpv-post-date format='U']' )"]
  Display Nothing
[/wpv-conditional]

I assume that in the view will be displayed nothing for the Books that published before the date of 'wpcf-my-date-and-time' custom field. For the posts that where published after that day will be displayed the title and the the featured image.

The issues:

1) I think that the pagination will be wrong as in the view query filters is set no filter and the view will search for all the Books but for the Books published before the date ' wpcf-my-date-and-time' it will just display nothing. This way I assume that a few blank pages will be generated.
2) I need to use the shortcode [wpv-found-count] that in this case, it will show the total number of Books. I need the shortcode to count only the Books that are published after the date and time field='wpcf-my-date-and-time' of the user's "Provider" post type.

Is there a way to pass the field's 'wpcf-my-date-and-time' value in a shortcode and then set the query filter to search posts according to the value of that shortcode? This way the shortcode [wpv-found-count] will show the right number and will be no blank pages generated. What do you think, is that achievable?

Thank you!

#529357

What has the Post Type "Provider" to do with this setup?

I see you added the Field to that Post Type, but if you don't query that post type, you cannot check upon it.

Anyway, you can also add this as a Query Filter, but of course only after the Field is either saved for the Books, or any form of relationship exists between the book and provider, so to call that Field with a $parent_post_type call.

The Query Filter has to be like this:

Select items with field:
Post Date is a number greater than or equal VIEW_PARAM(postdate)

The Views ShortCode has to be inserted with the GUI, which gives us this:

[wpv-view name="query-date" postdate="[wpv-post-date format="U"]"]

This will produce a list of Posts where the Post Field is greater date than the Post Publish Date.

The query can be adjusted as per needs, of course.

#529571

Hello Beda,

thank you for your answer! I need the user to see in a view different types of posts that have been published after the day and time he has set in a specific custom field of the post type "provider". But this is not possible if there is not a relationship between multiple post types and the post type "provider".

Could it be possible to pass in the query filter the value of a user field but search for posts? So if the user has chosen 25/05/2017 17:00 then he can see all the posts that have been published after that date and time. Is that possible?

Thank you!

#529646

I need the user to see in a view different types of posts that have been published after the day and time he has set in a specific custom field of the post type "provider".

Then you need to query that Post type.
There is no way for a WP Query to query by a Field that is not saved against the queried Post Type.

Could it be possible to pass in the query filter the value of a user field but search for posts?

No, in a Post View, you can query by Post Data.
In a User View, by User Data, and so on.
This is how the Query Code works in WordPress.

So if the user has chosen 25/05/2017 17:00 then he can see all the posts that have been published after that date and time. Is that possible?

I think you mean the current user, of course.
That is only possible with the Conditional HTML.
Example, used on a Post View:

[wpv-conditional if="( '[types usermeta='user-field-date' user_is_author='true' format='U'][/types]' eq '[wpv-post-date format="U"]' )"]
  [wpv-post-title]
[/wpv-conditional]

I display the Title only if the Post Date is equal to what the user selected in his own Date Field.
But as said, this is not a Query. This is a Conditional Display.

#529952

Dear Beda,

it's a bit comlicated what I am trying to achieve and propably is not possible with toolset plugins. I have to look for another way to achieve what I need. Anyway thank you very much for your time & your support !!!

Cheers

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