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

Conversation

Projects
None yet
4 participants
@DrewAPicture
Copy link

DrewAPicture commented Sep 7, 2014

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 some commits Sep 7, 2014

Add logic to handle a single date query + top-level arguments. Adds a…
… helper function, be_sanitize_date_time(), to DRY-up sanitization of date and time strings.
Add logic that attempts to salvage non-strictly-formatted dates and t…
…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'.
Add a third parameter to be_sanitize_date_time(), $accepts_string, fo…
…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.
The 'date_query_column' and 'date_query_compare' values don't need de…
…faults as they fallback to the values of the global 'column' and 'compare' arguments in WP_Date_Query.
@DrewAPicture

This comment has been minimized.

Copy link
Author

DrewAPicture commented Sep 7, 2014

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 some commits Sep 7, 2014

Make allowances for single-word relative date strings that don't cont…
…ain -. Kind of a weird way to go about it but the explode needs the hyphen to proceed.
@billerickson

This comment has been minimized.

Copy link
Owner

billerickson commented Sep 8, 2014

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

This comment has been minimized.

Copy link
Owner

billerickson commented Sep 10, 2014

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

This comment has been minimized.

Copy link
Author

DrewAPicture commented Sep 10, 2014

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

@billerickson

This comment has been minimized.

Copy link
Owner

billerickson commented Sep 10, 2014

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

This comment has been minimized.

Copy link
Author

DrewAPicture commented Sep 10, 2014

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

@billerickson

This comment has been minimized.

Copy link
Owner

billerickson commented Sep 10, 2014

That would be great, thank you.

@Lyallpuria

This comment has been minimized.

Copy link

Lyallpuria commented Dec 11, 2018

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

This comment has been minimized.

Copy link
Owner

billerickson commented Dec 11, 2018

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

@Lyallpuria

This comment has been minimized.

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

This comment has been minimized.

Copy link

SpiritSchool commented Dec 15, 2018

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

This comment has been minimized.

Copy link

SpiritSchool commented Dec 15, 2018

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

no working plz. tell new code of today.

@billerickson

This comment has been minimized.

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

This comment has been minimized.

Copy link

SpiritSchool commented Dec 15, 2018

Yes working. Thank you.

@Lyallpuria

This comment has been minimized.

Copy link

Lyallpuria commented Dec 17, 2018

[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

This comment has been minimized.

Copy link

Lyallpuria commented Dec 17, 2018

Yes working. Thank you.

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

@SpiritSchool

This comment has been minimized.

Copy link

SpiritSchool commented Dec 17, 2018

\

Yes working. Thank you.

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

I didnt check it with category attr.

@Lyallpuria

This comment has been minimized.

Copy link

Lyallpuria commented Dec 18, 2018

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

@SpiritSchool

This comment has been minimized.

Copy link

SpiritSchool commented Dec 18, 2018

hmmm. I'll. thanks Lyallpuria

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment