From cdb0546ef667fa994a8d80e287e4fc35bfcf8ab8 Mon Sep 17 00:00:00 2001 From: Deyan Vatsov Date: Tue, 19 Apr 2016 00:16:38 +0300 Subject: [PATCH] Allow filter by meta key/value --- CHANGELOG.md | 1 + README.md | 38 ++++++++++++++++++- .../plugin.php | 29 +++++++------- wp-rest-api-v2-custom-post-fields/readme.txt | 37 ++++++++++++++++++ wp-rest-api-v2-custom-post-types/readme.txt | 22 ----------- 5 files changed, 89 insertions(+), 38 deletions(-) rename {wp-rest-api-v2-custom-post-types => wp-rest-api-v2-custom-post-fields}/plugin.php (73%) create mode 100644 wp-rest-api-v2-custom-post-fields/readme.txt delete mode 100644 wp-rest-api-v2-custom-post-types/readme.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index 0563232..e4d4714 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,3 @@ # Changelog +* (29 April 2016). Allow filter by meta key/value. * (10 April 2016). Initial Release. diff --git a/README.md b/README.md index 8c98b92..b99a1ce 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,37 @@ -# WP REST API V2 Custom Post Types - +# WP REST API V2 Custom Post Fields +==== Shows Advanced Custom Field output to the WP REST API V2 for posts. + +https://wordpress.org/plugins/wp-rest-api-v2-custom-post-fields/ + +- [Installation](#installation) +- [Filters](#filters) + +Installation +==== + +This plugin requires having WP API installed and activated or it won't be of any use. + +*Manual Installation: +Upload the entire /wp-rest-api-v2-custom-post-fields directory to the /wp-content/plugins/ directory. + +*Better Installation: +Go to Plugins > Add New in your WordPress admin and search for 'WP REST API V2 Custom Post Fields'. + +Once installed, activate 'WP REST API V2 Custom Post Fields' from WordPress plugins dashboard page and you're ready to go. +This plugin works straight out of the box. + + +Filters +==== +| Endpoint | +|-----------| +| /wp-json/wp/v2/posts?filter[meta_key]=**{custom-field-name}**&filter[meta_value]=**{custom-field-value}** | + + + +If you want to limit the fields you have to add the allowed meta keys in 'allowed_meta_keys' array in `plugin.php` + +```PHP +$allowed_meta_keys = array( 'custom-field-name' ); +``` \ No newline at end of file diff --git a/wp-rest-api-v2-custom-post-types/plugin.php b/wp-rest-api-v2-custom-post-fields/plugin.php similarity index 73% rename from wp-rest-api-v2-custom-post-types/plugin.php rename to wp-rest-api-v2-custom-post-fields/plugin.php index 77a6a4d..6ba58ac 100644 --- a/wp-rest-api-v2-custom-post-types/plugin.php +++ b/wp-rest-api-v2-custom-post-fields/plugin.php @@ -1,11 +1,11 @@ ID, $field_name, strip_tags($value)); } - //Example: /wp-json/wp/v2/posts?filter[meta_key]=slideshow&filter[meta_value]=true&filter[cast]=boolean + //Example: /wp-json/wp/v2/posts?filter[meta_key]=slideshow&filter[meta_value]=true function allow_meta_query($valid_vars) { $valid_vars = array_merge( $valid_vars, array( 'meta_key', 'meta_value' ) ); return $valid_vars; @@ -67,19 +67,20 @@ function allow_meta_query($valid_vars) { function allowed_meta_keys($args) { // ToDo: to be configurable from admin panel + // set the allowed meta keys or asterisk for grant access $allowed_meta_keys = array( 'slideshow' ); - if ( $args['cast'] === "boolean" ) { - if ( $args['meta_value'] === "true" ) { - $args['meta_value'] = true; - } - if ( $args['meta_value'] === "false" ) { - $args['meta_value'] = false; - } + if ( isset($args['meta_value']) && $args['meta_value'] === "true" ) { + $args['meta_value'] = true; } - if ( !in_array( $args['meta_key'], $allowed_meta_keys ) ) { + if ( isset($args['meta_value']) && $args['meta_value'] === "false" ) { + $args['meta_value'] = false; + } + + + if ( $allowed_meta_keys[0] !== '*' && isset($args['meta_key']) && !in_array( $args['meta_key'], $allowed_meta_keys ) ) { unset( $args['meta_key'] ); unset( $args['meta_value'] ); } diff --git a/wp-rest-api-v2-custom-post-fields/readme.txt b/wp-rest-api-v2-custom-post-fields/readme.txt new file mode 100644 index 0000000..dc26893 --- /dev/null +++ b/wp-rest-api-v2-custom-post-fields/readme.txt @@ -0,0 +1,37 @@ +=== Plugin Name === +Contributors: Deyan Vatsov +Tags: api, json, REST, rest-api-v2, advanced custom post fields, ACF +Requires at least: 4.3 +Tested up to: 4.4.5 +Stable tag: 0.1 +License: GPLv2 or later +License URI: http://www.gnu.org/licenses/gpl-2.0.html + +Shows Advanced Custom Field output to the WP REST API V2 for posts. + +== Description == + +This plugin combines the two of the best WordPress plugins: [Advanced Custom Fields](https://wordpress.org/plugins/advanced-custom-fields/ "Advanced Custom Fields") and [WP REST API](https://wordpress.org/plugins/rest-api/ "WP REST API"). + +**See details on GitHub:** https://github.com/Vatsov/wp-rest-api-v2-custom-post-fields/ + +== Installation == + +This plugin requires having WP API installed and activated or it won't be of any use. + +*Manual Installation: +Upload the entire /wp-rest-api-v2-custom-post-fields directory to the /wp-content/plugins/ directory. + +*Better Installation: +Go to Plugins > Add New in your WordPress admin and search for 'WP REST API V2 Custom Post Fields'. + +Once installed, activate 'WP REST API V2 Custom Post Fields' from WordPress plugins dashboard page and you're ready to go. +This plugin works straight out of the box. + +== Changelog == + += 0.2 = +* Allow filter by meta key/value + += 0.1 = +* Initial Release. diff --git a/wp-rest-api-v2-custom-post-types/readme.txt b/wp-rest-api-v2-custom-post-types/readme.txt deleted file mode 100644 index 8ee2d6f..0000000 --- a/wp-rest-api-v2-custom-post-types/readme.txt +++ /dev/null @@ -1,22 +0,0 @@ -=== Plugin Name === -Contributors: Deyan Vatsov -Tags: api, json, REST, rest-api-v2, advanced custom post types, ACF, custom post types -Tested up to: 4.4.5 -Stable tag: 0.1 -License: GPLv2 or later -License URI: http://www.gnu.org/licenses/gpl-2.0.html - -Shows Advanced Custom Field output to the WP REST API V2 for posts. - -== Description == - -This plugin combines the two of the best WordPress plugins: [Advanced Custom Fields](https://wordpress.org/plugins/advanced-custom-fields/ "Advanced Custom Fields") and [WP REST API](https://wordpress.org/plugins/rest-api/ "WP REST API"). - -== Installation == - -This plugin works straight out of the box. - -== Changelog == - -= 0.1 = -* Initial Release.