-
-
Save cruzmayra/075b49ade44a19d22013bc8764606d80 to your computer and use it in GitHub Desktop.
<?php | |
add_filter( 'rest_ajde_events_query', 'event_filter_by_month_and_year', 20, 2 ); | |
/** | |
* @example http://tudominio.test/wp-json/wp/v2/ajde_events?year=2020&month=3 | |
*/ | |
function event_filter_by_month_and_year($args, $request) { | |
$year = strval(intval($request['year'], 10)); | |
$month = strval(intval($request['month'], 10)); | |
$args += [ | |
'meta_query' => [ | |
'relation' => 'AND', | |
[ | |
'key' => 'event_year', | |
'value' => $year, | |
'compare' => '=' | |
], | |
[ | |
'key' => '_event_month', | |
'value' => $month, | |
'compare' => '=' | |
] | |
] | |
]; | |
return $args; | |
} |
I don't know @ravimallya it feels like the complexity of the function exploded, it feels a bit messy, don't you?
It's a nice feature tough being able to pass a filter by several years or months, however one I wouldn't find too much realistic.
The filter by month and year specific to one month, one year works to provide the events to a custom calendar with monthly view. I don't know how a range of several months could be combined across several years. You could say to compare the events on the same range of months from one year to another but it feels like a forced use case to me.
About the filter by month not by number but an english word, I don't know if I like it better or not. I don't even know if it is supported by AJDE, maybe recent versions. I'm not working in that project anymore.
We cam pass either single values or comma-separated values for both year and month in the API query, and it will filter posts accordingly. For example:
http://tudominio.test/wp-json/wp/v2/ajde_events?year=2020&month=january,february,march
will filter posts for the specified years and months.http://tudominio.test/wp-json/wp/v2/ajde_events?year=2020,2021&month=january
will filter posts for the specified year and month.