Skip to content

Commit

Permalink
Allow filter by meta key/value
Browse files Browse the repository at this point in the history
  • Loading branch information
Deyan Vatsov authored and Vatsov committed Apr 29, 2016
1 parent 7cba78c commit cdb0546
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Changelog
* (29 April 2016). Allow filter by meta key/value.
* (10 April 2016). Initial Release.
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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' );
```
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

/**
* Plugin Name: WP REST API V2 Custom Post Types
* Description: Adds Advanced Custom Post Types to WP REST API V2 JSON output.
* Version: 0.1
* Plugin Name: WP REST API V2 Custom Post Fields
* Description: Adds Advanced Custom Post Fields to WP REST API V2 JSON output.
* Version: 0.2
* Author: Deyan Vatsov
* Plugin URI: https://github.com/Vatsov/wp-rest-api-v2-custom-post-types/
* Plugin URI: https://github.com/Vatsov/wp-rest-api-v2-custom-post-fields/
*/

if ( !function_exists( 'is_plugin_active' ) ) {
Expand All @@ -19,7 +19,7 @@
class CustomPostDataPlugin {
public function __construct() {
// Add Meta Fields to Posts
add_action('rest_api_init', array( $this, 'add_custom_data' ) );
add_action('rest_api_init', array( $this, 'add_custom_data' ) );
add_filter('rest_query_vars', array( $this, 'allow_meta_query' ) );
add_filter('rest_post_query', array( $this, 'allowed_meta_keys' ) );
}
Expand Down Expand Up @@ -59,27 +59,28 @@ function update_custom_data($value, $post, $field_name) {
return update_post_meta($post->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;
}

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'] );
}
Expand Down
37 changes: 37 additions & 0 deletions wp-rest-api-v2-custom-post-fields/readme.txt
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 0 additions & 22 deletions wp-rest-api-v2-custom-post-types/readme.txt

This file was deleted.

0 comments on commit cdb0546

Please sign in to comment.