Title: example using date_query
Last modified: September 19, 2021

---

# example using date_query

 *  Resolved [irving.bos](https://wordpress.org/support/users/irvingbos/)
 * (@irvingbos)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/example-using-date_query/)
 * Hi, I am unable to create a mla_gallery query that pulls all media posted “last
   year”. In fact, I cant get anything using date_query to work.
    Would anyone have
   a quick example of the syntax?
 * thanks!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [David Lingren](https://wordpress.org/support/users/dglingren/)
 * (@dglingren)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/example-using-date_query/#post-14895682)
 * Thanks for your post. I regret the trouble you’re having with the date query.
 * You did not give any examples of what you tried so I can’t comment on your specific
   problem(s). Here are three examples that are working well on my test system:
 *     ```
       <h3>Before Last Year</h3>
   
       [mla_gallery numberposts=8 orderby="date DESC" mla_caption="{+post_date+}" date_query="array( array( 'before' => 'last year' ) )"]
   
       <h3>Year 2020</h3>
   
       [mla_gallery date_query="array( array( 'year' => 2020 ) )"]
   
       <h3>One Year, Before Last Year</h3>
   
       [mla_gallery numberposts=8 orderby="date ASC" mla_caption="{+post_date+}" date_query="array( array( 'before' => 'last year', 'after' => '2 years ago' ) )"]
       ```
   
 * The second example is closest to your **_all media posted “last year”_** requirement.
   The other two examples translate “last year” and “2 years ago” to a specific 
   date and time, e.g., today the SQL query for example three is:
 *     ```
       AND ( ( mladev_posts.post_date > '2019-09-21 15:50:22'
       AND mladev_posts.post_date < '2020-09-21 15:50:22' ) )
       ```
   
 * I can find no way to express a query for “the entire year before the current 
   year”. In particular, **DO NOT CODE ANYTHING LIKE** `date_query="array( array('
   year' => 'last year' ) )"` WordPress does not validate the `year` value and giving
   it a string causes a fatal PHP error on the post/page where the string appears.
   You have to delete the post/page and start over from scratch. I learned that 
   the hard way.
 * You will have to edit the query once each year as part of your new year’s resolutions.
   You could always use the hooks MLA provides to write a bit of code that does 
   the calculation and updates the query automatically. If that’s of interest I 
   can give you more specific help.
 * The latest version of the “MLA UI Elements Example” plugin includes a new `[muie_archive_list]`
   shortcode for date-based archive lists and dropdown controls. You can find it
   on the Settings/Media Library Assistant Documentation tab by clicking “Example
   Plugins”.
 * I hope that gets you going on formulating `date_query` values for your application.
   I am marking this topic resolved, but please update it if you have any problems
   or further questions regarding the above examples. Thanks for your interest in
   the plugin.
 *  Thread Starter [irving.bos](https://wordpress.org/support/users/irvingbos/)
 * (@irvingbos)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/example-using-date_query/#post-14898316)
 * Thanks david, that really does help me a lot! Thanks for confirming that there’s
   no real proper way to code anything that does not need to be revised once per
   year; I ran into the same problem you did.
    For sure you can mark this topic 
   is closed, and let me say thanks for the great in-depth support you offer for
   this plugin. There are very few plugins out there that are free and yet carry
   the level of complexity and support that this one does.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘example using date_query’ is closed to new replies.

 * ![](https://ps.w.org/media-library-assistant/assets/icon-256x256.png?rev=973502)
 * [Media Library Assistant](https://wordpress.org/plugins/media-library-assistant/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/media-library-assistant/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/media-library-assistant/)
 * [Active Topics](https://wordpress.org/support/plugin/media-library-assistant/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/media-library-assistant/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/media-library-assistant/reviews/)

## Tags

 * [date_query](https://wordpress.org/support/topic-tag/date_query/)
 * [mla_gallery](https://wordpress.org/support/topic-tag/mla_gallery/)

 * 2 replies
 * 2 participants
 * Last reply from: [irving.bos](https://wordpress.org/support/users/irvingbos/)
 * Last activity: [4 years, 6 months ago](https://wordpress.org/support/topic/example-using-date_query/#post-14898316)
 * Status: resolved