|
| 1 | +<?php |
| 2 | +/* |
| 3 | +Plugin Name: Get by taxonomy/category parent for WP REST API |
| 4 | +Description: Add the ability to get posts by the parent category in the WordPress REST API. |
| 5 | +Author: M Media |
| 6 | +Version: 1.0.0 |
| 7 | +Author URI: https://profiles.wordpress.org/Mmediagroup/ |
| 8 | +License: GPL2 |
| 9 | +License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 10 | +
|
| 11 | +{Plugin Name} is free software: you can redistribute it and/or modify |
| 12 | +it under the terms of the GNU General Public License as published by |
| 13 | +the Free Software Foundation, either version 2 of the License, or |
| 14 | +any later version. |
| 15 | +
|
| 16 | +{Plugin Name} is distributed in the hope that it will be useful to Cartes.io clients, |
| 17 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +GNU General Public License for more details. |
| 20 | +
|
| 21 | +You should have received a copy of the GNU General Public License |
| 22 | +along with {Plugin Name}. If not, see {License URI}. |
| 23 | + */ |
| 24 | + |
| 25 | +defined('ABSPATH') or die('No script kiddies please!'); |
| 26 | + |
| 27 | +// Add parent_category filter to REST API |
| 28 | +if (!function_exists('wprabpc_wp_rest_api_by_parent_category')) { |
| 29 | + function wprabpc_wp_rest_api_by_parent_category($args, $request) |
| 30 | + { |
| 31 | + if (isset($request['parent_category'])) { |
| 32 | + $parent_category = sanitize_text_field($request['parent_category']); |
| 33 | + $args['tax_query'] = [ |
| 34 | + [ |
| 35 | + 'taxonomy' => 'category', |
| 36 | + 'field' => 'term_id', |
| 37 | + 'include_children' => true, |
| 38 | + 'operator' => 'IN', |
| 39 | + 'terms' => $parent_category, |
| 40 | + ], |
| 41 | + ]; |
| 42 | + } |
| 43 | + return $args; |
| 44 | + } |
| 45 | + add_filter('rest_post_query', 'wprabpc_wp_rest_api_by_parent_category', 10, 3); |
| 46 | +} |
0 commit comments