WordPress.org

Ready to get started?Download WordPress

Forums

JSON REST API (WP API)
Filter by meta key/value (11 posts)

  1. Geet Jacobs
    Member
    Posted 8 months ago #

    I am trying to filter by meta and not having much luck. I have been able to filter the order, post count and other things. This is for a custom post type, so not sure if that is the issue.

    This is what I am trying to use, filter my meta_key of artist and meta_value of the artists ID.
    /wp-json.php/posts?type=artwork&filter[meta_key]=artist&filter[meta_value]=264

    I also read about allow public meta, so tested adding the below code as the custom meta/fields are form advanced custom fields.

    add_filter( 'rest_api_allowed_public_metadata', 'jeherve_allow_fb_metadata' );
    function jeherve_allow_fb_metadata() {
        // only run for REST API requests
        if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST )
            return $allowed_meta_keys;
    
        $allowed_meta_keys[] = '_artist';
           $allowed_meta_keys[] = 'artist';
    
        return $allowed_meta_keys;
    }

    Cheers!

    http://wordpress.org/plugins/json-rest-api/

  2. Jeremy Herve
    Happiness Engineer
    Posted 8 months ago #

    The code you mentioned probably won't work with the JSON REST API plugin. It was built for the JSON API module in Jetpack.

    The JSON REST API plugin doesn't include the rest_api_allowed_public_metadata filter.

  3. Geet Jacobs
    Member
    Posted 8 months ago #

    Thank you Jeremy, I dropped that function.

    Do you happen to know what I am doing wrong, is meta filtering not built in yet?

    Looking through the code I found this below line that might need a filter?

    $valid_vars = apply_filters('json_query_vars', $valid_vars);

    Something like...

    add_filter( 'json_query_vars', 'anagram_add_meta_filter' );
    function anagram_add_meta_filter() {
        // only run for REST API requests
        if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST )
            return $filter_meta_keys;
    
        $filter_meta_keys[] = 'meta_value';
    
        return $filter_meta_keys;
    }

    In the documentation I see it says that the filters use the default WP_query options.
    Doc info: Here

    "The last parameter is the filter parameter. This gives you full access to the WP_Query parameters, to alter the query to your liking. Depending on the level of access you have, not all parameters will be available, so check the schema for the available parameters."

    Checking the schema parameters shows post_meta as an option.

    Thanks for any help on this.

  4. Jeremy Herve
    Happiness Engineer
    Posted 8 months ago #

    I'm not at all familiar with the JSON REST API plugin, so I won't be of much help here I'm afraid. Ryan will probably be able to help :)

  5. Ryan McCue
    Member
    Plugin Author

    Posted 8 months ago #

    The plugin internally uses the is_protected_meta() function, which has the is_protected_meta filter.

    If that doesn't work, let me know. :)

  6. Geet Jacobs
    Member
    Posted 8 months ago #

    Thank you Ryan,

    I commented out the is_protected_meta filter in the plugin to test. Even after doing so i am not able to filter by meta_key / meta_value.

    I have a custom post type of artwork with a meta_key of artist with a meta_key of 264, i am using the below api call to try to get the artwork post type with that meta.

    http://192.168.222.50/pwg/wp-json.php/posts?type=artwork&filtermeta_key=artist&filtermeta_value=264

    Even with the above filters I still get "all" the artwork post type,s just just the ones with that certain meta.

    The artist meta field shows in my admin so I do not think its a protected field issue even though it was created with Advanced Custom Fields.

    Thanks for any help on filtering by meta.

  7. Dalton
    Member
    Posted 7 months ago #

    I'm having trouble with meta filtering too. Some additional documentation on this would be super helpful!

  8. dunar21
    Member
    Posted 7 months ago #

    You have to hook into json-valid-vars

    $valid_vars = apply_filters('json_query_vars', $valid_vars);

    so:

    function addsomevars($filters){
       $metaparts = array("meta_key", "meta_value", "meta_compare", "meta_query");
       $filters = array_merge($filters, $metaparts);
       return $filters;
    }
    add_filter('json_query_vars', 'addsomvars');

    If you wanted to make sure "meta_query" worked right, you could use the additional filter here:

    $query[ $var ] = apply_filters( 'json_query_var-' . $var, $filter[ $var ] );

    That could look something like this where you pass the meta_query value as serialized data (from maybe_serialize):

    function custom-filtermetq($val){
       return maybe_unserialize($val);
    }
    add_filter('json_query_var-meta_query', 'custom-filtermetq');

    the param might look something like this:
    &filter[meta_query]=a%3A1%3A%7Bi%3A0%3Ba%3A3%3A%7Bs%3A3%3A%22key%22%3Bs%3A5%3A%22color%22%3Bs%3A5%3A%22value%22%3Bs%3A4%3A%22blue%22%3Bs%3A7%3A%22compare%22%3Bs%3A8%3A%22NOT+LIKE%22%3B%7D%7D

    The above is urlencoded from : a:1:{i:0;a:3:{s:3:"key";s:5:"color";s:5:"value";s:4:"blue";s:7:"compare";s:8:"NOT LIKE";}}

    NOTE: not tested, just wrote it. Play with these hooks and you'll get the filtering you want. Remember to urlencode the data. It seems that maybe_unserialize decodes the url just fine, but you may want to check up on that.

  9. Geet Jacobs
    Member
    Posted 7 months ago #

    Thanks Dunar,

    I tried both of those and tweaked bits and pieces. Also encoded the meta_query url to specify artist meta_key with the meta_value of the artists ID.

    Still no luck on the filtering. I am surprised more people have not needed this to work even on the git repository. Maybe I should switch tot he other REST json plugin as it has these features. I wanted to future proof the site I was working on those since this plugin is planed to be added to the core.

  10. dunar21
    Member
    Posted 7 months ago #

    Yeah, this plugin is "planning" on being added to the core. Not so sure that's going to happen, but I feel the same way... better safe than sorry. If you want to do something custom, just extend the api with a class and do whatever you want with it. For an example of extending it, I wrote an extension to allow user registration. You can see the code here: http://wordpress.org/support/topic/registration-and-examples?replies=3

    You don't have to rely on any limitations or restrictions when doing it that way, just be sure that you are careful about who is logging in, gathering and using your parameters correctly, etc...

  11. mazong1123
    Member
    Posted 1 week ago #

    Find a line in get_posts function:

    // Allow the same as normal WP
    $valid_vars = apply_filters('query_vars', $wp->public_query_vars);

    Add following code below it:

    // Add additional key to support.
    array_push($valid_vars, 'meta_key');

    Done :)

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.