Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add date_query support #77

Merged
merged 14 commits into from
Sep 10, 2014
Merged

Conversation

DrewAPicture
Copy link

As requested in #38, this PR implements date_query support for a single date query.

This would be considered "phase 1" with the option to later add support for incremented (multiple) date queries. The reasoning for not including multiples with this PR, is that while the logical progression is to move to supporting multiples in the future, it would probably be best to first nail down the best approach to implementing a single date query in terms of attribute naming and architecture :)

Note: In the attribute descriptions below, you'll notice that I reference the "top-level" and "second-level" areas of the date query. Top-level arguments serve in a "global" capacity to all date queries contained on the second-level. The actual individual date queries are defined on the second-level of the associative array.

This introduces a total of 8 new attributes (yikes, I know):

  • date
    — Serves as a shortcut to setting the 'year', 'month', and 'day' arguments for a given date query struct on the second-level. It accepts either a strictly-formatted 'YYYY-MM-DD' date string or a relative-formatted date string, such as 'Sunday, September 7'. The relative-formatted string is intended as a fallback for the easier-to-sanitize 'YYYY-MM-DD' formatted string and could prove fickle when supplied with too little information.
  • date_column
    — The first of two top-level arguments, this serves as the "global" column to query for all date queries in the date query arrays on the second-level. This defaults to 'post_date'.
  • date_compare
    — The second of two top-level arguments, this serves as the "global" comparison operator for all date queries in the date query arrays on the second-level. This defaults to '='.
  • date_query_before
    — Sets the 'before' argument for a date query on the second level. It accepts either a strictly-formatted date, 'YYYY-MM-DD', or as a fallback, a relative-formatted date string (using the same logic as the date attribute).
  • date_query_after
    — Sets the 'after' argument for a date query on the second level. Like the date_query_before attribute, It accepts either a a strictly-formatted date, 'YYYY-MM-DD', or as a fallback, a relative-formatted date string (using the same logic as the date attribute.
  • date_query_column
    — Sets the column to query by for the second-level date query. If not set, falls back to the value of date_column.
  • date_query_compare
    — Sets the comparison operator for the second-level date query. If not set, falls back to the value of date_compare.
  • time
    — Serves as a shortcut to populating the 'hour', 'minute', and 'second' arguments in the second-level date query. It accepts only a strictly-formatted string in the format of 'HH:MM:SS' or 'HH:MM'. This does NOT have a string falback.

It also introduces a new function, be_sanitize_date_time() for handling date and time sanitization, as this logic is reused in 3 or 4 different instances, I thought it best to keep it DRY :)

be_sanitize_date_time() includes a filter though it does not include the early-return-fallback logic at the top.

Here are some sample shortcodes:
Query for posts published on a specific date:

[display-posts date="2014-09-07"]
[display-posts date="Sunday, September 9, 2014"]

Query for posts published after January 1, 2013:

[display-posts date_query_after="2013-01-01"]
[display-posts date_query_after="January 1, 2013"]

Query for posts published BEFORE today:

[display-posts date_query_before="Today"]

Query for posts modified yesterday:

[display-posts date="Yesterday" date_query_column="post_modified"]

DrewAPicture added 12 commits September 7, 2014 12:54
… helper function, be_sanitize_date_time(), to DRY-up sanitization of date and time strings.
… the simplifcation change two commits ago.
…imes.

This opens the door to using 'relative' date and time formats such as 'Wednesday August 6' with the year implied instead of something like '2014-08-06'.
…r whether the calling argument check can accept a string.

This would only work as a fallback for the 'date', 'date_query_before' or 'date_query_after' attributes. The 'date' argument simply serves as a quick shortcut to defining the 'year', 'month', and 'day' arguments for any given date_query array.
…faults as they fallback to the values of the global 'column' and 'compare' arguments in WP_Date_Query.
@DrewAPicture
Copy link
Author

I'm a little on the fence about the attribute naming. It's clear there needs to be a logical division between the top-level and second-level attributes, though things could easily get pretty long once you start thinking about 1) using multiple date query attributes in a single shortcode 2) eventually incrementing those attributes for multiple date queries.

In the vein of multiple queries, we could probably forgo introducing the second-level date_query_column and date_query_compare attributes until "phase 2", as they really are only used for fine-tuning of the individual date queries when there are multiples.

"phase 2" would then introduce date_query_column and date_query_compare (assuming the naming convention is kept), plus a 9th attribute, date_relation and of course the "magic" incremented attributes like date_query_before_2 et al. pulled directly from $original_atts.

Something to think about.

DrewAPicture added 2 commits September 7, 2014 16:39
…ain -. Kind of a weird way to go about it but the explode needs the hyphen to proceed.
@billerickson
Copy link
Owner

Thanks for this. It will be a few days before I have the opportunity to review this, but I'm excited to play with it.

@billerickson
Copy link
Owner

I got syntax errors from the variables defined within an if statement. Ex: https://github.com/DrewAPicture/display-posts-shortcode/blob/datequery/display-posts-shortcode.php#L164 Breaking it apart into $variable = whatever; if ( ! empty( $variable ) ) fixed it for me.

But other than that, it worked great!

billerickson added a commit that referenced this pull request Sep 10, 2014
@billerickson billerickson merged commit fbcc71e into billerickson:master Sep 10, 2014
@DrewAPicture
Copy link
Author

Boom. What were your thoughts on variable if/when support for multiple date queries comes up (assuming it will)?

@billerickson
Copy link
Owner

I think we should approach it in the same way multiple tax queries are currently done.

I'm thinking I'll push this version out once I take care of #75, and then if there's a need for multiple date queries, we can add that in the next version.

@DrewAPicture
Copy link
Author

I'm happy to supplement the wiki for the date query stuff added as well.

@billerickson
Copy link
Owner

That would be great, thank you.

@Lyallpuria
Copy link

How to display the posts of only "Current Day"?
Using specific date, i can achieve my object but i need to change the date daily.

Please urgent reply

@billerickson
Copy link
Owner

@Lyallpuria [display-posts date="today"]

@Lyallpuria
Copy link

Lyallpuria commented Dec 12, 2018

I have applied in this date code in the following way

[display-posts date="today" category="Dawn" no_posts_message="No Job Published in Dawn Newspapers Today." posts_per_page="150"]

but this is not working so I have applied the following as usual

[display-posts date="2018-12-12" category="Dawn" no_posts_message="No Job Published in Dawn Newspapers Today." posts_per_page="150"]

I am using IsleMag theme and This plugin has not been tested with version of WordPress.

Plz spend your precious time for this issue. And tell me how to fix it....

thanks

@SpiritSchool
Copy link

How to display the posts of only "Current Day"?
Using specific date, i can achieve my object but i need to change the date daily.

Please urgent reply

this code has not worked on my website. if there is any other code, then please tell me

@SpiritSchool
Copy link

@Lyallpuria [display-posts date="today"]

no working plz. tell new code of today.

@billerickson
Copy link
Owner

billerickson commented Dec 15, 2018

Try [display-posts date_query_after="yesterday"]

I just tested and confirmed that works.

If you need additional help resolving your issue, I recommend you hire a developer on Codeable

@SpiritSchool
Copy link

Yes working. Thank you.

@Lyallpuria
Copy link

[display-posts date_query_after="yesterday"]
its working but when you insert category attribute, it shows also yesterday posts

I have applied the following code and see the result in attached picture
[display-posts Category="dunya" date_query_after="Yesterday" posts_per_page="150" include_date="true" ]

ice_screenshot_20181217-154646

@Lyallpuria
Copy link

Yes working. Thank you.

Spirit, yes its working but please test with category attribute.

@SpiritSchool
Copy link

\

Yes working. Thank you.

Spirit, yes its working but please test with category attribute.

I didnt check it with category attr.

@Lyallpuria
Copy link

Lyallpuria commented Dec 18, 2018

@SpiritSchool Try "List category posts" plugin
[catlist after="-1 days"]

@SpiritSchool
Copy link

hmmm. I'll. thanks Lyallpuria

@brunogabuzomeu
Copy link

This syntax is working:
[display-posts date_query_after="-1 months"]

@magnetarsaurus
Copy link

Hi there,

I'm using your plugin (thank you!!) and implemented it as follows:
[display-posts date_query_before="-1 days" date_query_after="-2 days"]
[display-posts date_query_before="-2 days" date_query_after="-3 days"]
[display-posts date_query_before="-3 days" date_query_after="-4 days"]

etc etc, hoping that i would get a list of posts for every day of the week.

The problem is, that during the day some posts move to the column of the previous day (rather than wait until 0:00h).

Do you have any idea what causes this behaviour? If so, is there a solution for it?

Thanks in advance!

Best,
Robin

@juanalan
Copy link

juanalan commented Oct 6, 2024

Hello guys
I need some help on the plugin, connected to the WP Events Manager plugin.

I use in shortcodes date_query_after:
[display-posts post_type=”event_listing” s=”Tartempion” orderby=”_event_start_date” date_query_after=”today”]

But it doesn’t work ! Same for the date_query_before !
Is it because the date_query_xxx item uses only the modification date ? or publication date ?
How can I use date de date_query_xxx arguments with the event_start_date ?

Thx a lot for your help

JA

@billerickson
Copy link
Owner

@juanalan Date queries in WordPress are for the published date or modified date.

WP Events Manager is likely storing the event date as metadata, so you should use a meta query. Here are some examples:

I recommend contacting WP Events Manager to find out the proper meta keys for querying their data.

@juanalan
Copy link

juanalan commented Oct 7, 2024

hello Billy
Thanks for your answer. Until now, they haven't tell me useful informations. But I will try again.
Nevertheless, even with the Events Calendar plugin, your example doesn't seem to work (I use your code in a snippet to test it).
An issue ?

Thanks again for your help

JA

@billerickson
Copy link
Owner

Those code snippets were functional when written in 2019. I'm not sure if the post type or meta key has changed.

The general idea is you need to query based on the post type (ex: tribe_events ) and the meta field that stores the event date (ex: _EventStartDate)

@juanalan
Copy link

juanalan commented Oct 7, 2024

The post_type is "event_listing" and the event date seems to be "event_start_date" (or "_event_start_date").
Nevertheless, I don't understand how to do with the examples.
Can you help me ?
JA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants