Skip to content

Instantly share code, notes, and snippets.

@danielmartins
Created March 16, 2013 01:29
Show Gist options
  • Save danielmartins/5174497 to your computer and use it in GitHub Desktop.
Save danielmartins/5174497 to your computer and use it in GitHub Desktop.
Wordpress: Get posts between dates based on specific post meta field
<?php
/**
* Get posts in range date by a specific custom field(post meta)
* But, the date needs to be in the format YYYYMMDD
*
* @param string $start start date
* @param string $end end date
* @param string $field specific custom field
* @param string $ctype
* @return array of posts
*/
function get_posts_between($start, $end, $field, $ctype = 'post')
{
$args = ['post_type' => $ctype,
'meta_query' => [
[
'key' => $field,
'value' => [$start, $end],
'compare' => 'BETWEEN',
'type' => 'DATE'
]
]
];
$query = new WP_Query($args);
return $query->get_posts();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment