Skip to content

Commit 6b95b91

Browse files
authored
Add files via upload
0 parents  commit 6b95b91

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

WPRestApiByParentCat.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}

readme.txt

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
=== Get by taxonomy/category parent for WP REST API ===
2+
Contributors: mmediagroup
3+
Donate link: https://github.com/sponsors/M-Media-Group
4+
Tags: rest-api, api, wp-api
5+
Requires at least: 4.6
6+
Tested up to: 5.6
7+
Requires PHP: 7.2
8+
Stable tag: trunk
9+
License: GPLv2 or later
10+
License URI: http://www.gnu.org/licenses/gpl-2.0.html
11+
12+
Add the ability to get posts by the parent category in the WordPress REST API.
13+
14+
== Description ==
15+
16+
Add the ability to get posts by the parent category in the WordPress REST API.
17+
18+
= Usage =
19+
20+
You can use the new endpoint with: `/wp-json/wp/v2/posts?parent_category=[XXX]`
21+
22+
== Features ==
23+
24+
1. 100% free
25+
2. Get posts by the category they belong in (including parent, grandparent, etc categories)
26+
27+
== Installation ==
28+
29+
1. Search for the "Get by taxonomy/category parent for WP REST API" plugin in Plugins > Add New.
30+
2. Press "Install Now".
31+
3. When the installation is complete, press "Activate plugin".
32+
33+
== Frequently Asked Questions ==
34+
35+
= Is it free? =
36+
37+
Yes, this plugin is free to use.
38+
39+
== Changelog ==
40+
41+
= 1.0 =
42+
* Initial commit.
43+
44+
== Upgrade Notice ==
45+
= 1.0 =
46+
If you're not on 1.0 - are you really living?
47+
48+
== Privacy policy ==
49+
This plugin works locally. No data is sent to other servers, or stored anywhere.

0 commit comments

Comments
 (0)