Skip to content

Instantly share code, notes, and snippets.

@cruzmayra
Created February 7, 2020 22:23
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cruzmayra/075b49ade44a19d22013bc8764606d80 to your computer and use it in GitHub Desktop.
Save cruzmayra/075b49ade44a19d22013bc8764606d80 to your computer and use it in GitHub Desktop.
Filtra custom_post_type según un par de campos.
<?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;
}
@ravimallya
Copy link

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.

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=january,february,march
 * @example http://tudominio.test/wp-json/wp/v2/ajde_events?year=2020&month=january
 */
function event_filter_by_month_and_year($args, $request) {
    $years = isset($request['year']) ? explode(',', $request['year']) : array();
    $months = isset($request['month']) ? explode(',', $request['month']) : array();

    // Ensure that $years and $months are non-empty arrays before adding to the meta query
    if (!empty($years) || !empty($months)) {
        $meta_query = array('relation' => 'AND');

        if (!empty($years)) {
            $year_comparison = (count($years) > 1) ? 'IN' : '=';
            $meta_query[] = array(
                'key'     => 'event_year',
                'value'   => $years,
                'compare' => $year_comparison,
            );
        }

        if (!empty($months)) {
            $month_comparison = (count($months) > 1) ? 'IN' : '=';
            $meta_query[] = array(
                'key'     => '_event_month',
                'value'   => $months,
                'compare' => $month_comparison,
            );
        }

        $args['meta_query'] = $meta_query;
    }

    return $args;
}

@tzkmx
Copy link

tzkmx commented Sep 13, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment