Reputation: 33
I need a query to get all posts that date custom field are before a month sended by URL var. Custom field saves data like YYYYMMDD (20150330). I want to get all post before this year/month (201503).
I try this but it doesn't works, because date_added content it isn't YYYYMM. It contains day too.
$year = $_GET['year'];
$month = $_GET['mes'];
$yearmonth = $month." ".$year;
$args = array (
'post_type' => 'clients',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'post_title',
'meta_query' => array(
array(
'key' => 'client_type',
'compare' => '=',
'value' => 'Si',
),
array(
'key' => 'date_added',
'compare' => '<=',
'value' => date("Ym", strtotime($yearmonth)),
)
));
$posts = get_posts($args);
Anyone can help me with this?
Thanks
Upvotes: 0
Views: 474
Reputation: 15301
If the date is stored in the database as an integer, then look where date less than date("Ym00", strtotime($yearmonth))
(zeros for day) for everything before March-1 or if you wanted the month of march use "Ym99"
to signify a date in march that is greater than the last day but still less than the following month. The date 20150301
as an integer translates to 20,150,301
(twenty million, one hundred fifty thousand, three hundred and one). No date will be below 201,503
so you need to add the day numbers to make them in the same scale.
Upvotes: 1