-
-
Save eduardoleao/f7c17e33b620e1bec142332ba3cd2370 to your computer and use it in GitHub Desktop.
Wordpress: Get posts between dates based on specific post meta field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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