1

Hi I'm using the great JSON API for Wordpress and would like to filter my custom post types by a specific custom field (using the meta_key and meta_value params).

The query in my URL looks like this so far:

http://example.org?json=get_recent_posts&post_type=mytype&custom_fields=myfield&include=title,custom_fields&meta_key=myfield&meta_value=myvalue

However, it still returns all the posts (from that post type), no matter whether or not they contain the custom field mapping.

What's wrong? Thanks in advance for any help.

3
  • You should show some code - actually the one with which you're trying to receive the post.
    – kaiser
    Commented Feb 17, 2012 at 10:18
  • Well, if you know the WP JSON API - that's all there is. After installing the plugin, you can request JSON data by typing a URL such as written in my question.
    – saschoar
    Commented Feb 17, 2012 at 10:25
  • I don't know it and I guess there are not too many people who know it, so I'll just guess. See answer.
    – kaiser
    Commented Feb 17, 2012 at 10:37

1 Answer 1

1

As this is too much for a comment, I'll post here and delete later:

http://example.org // domain
?json=get_recent_posts // controller
# Arguments for the query start here
&post_type=mytype 
&custom_fields=myfield
&include=title,custom_fields
&meta_key=myfield
&meta_value=myvalue

Have you tried the following?

  • Debug the response: http://www.example.org/api/get_page_index/?dev=1
  • Widget-style JSONP output: http://www.example.org/api/get_recent_posts/?callback=show_posts_widget&read_more=More&count=3
  • Redirect on error: http://www.example.org/api/posts/create_post/?callback_error=http%3A%2F%2Fwww.example.org%2Fhelp.html

And could you post what you get out from the debug?

5
  • After adding the &dev=1 param to my query, the output is completely the same (formatted though, but no errors or anything). The other methods don't help much for my problem. Here I read that the meta_key and meta_value are not public query vars, so I'll probably have to map custom query vars manually as it is suggested in the first answer.
    – saschoar
    Commented Feb 17, 2012 at 10:52
  • P.S. but I was hoping for an easier solution...
    – saschoar
    Commented Feb 17, 2012 at 10:58
  • I'd suggest to intercept the 'posts_clauses' filter and var_dump() the input $pieces. That gives you more insights about what happens in the query. Actually it will show you the query and you can there modify it.
    – kaiser
    Commented Feb 17, 2012 at 11:19
  • Thanks! As you suggested here, I did the var_dump and this is the output. No custom-field filters there. As I said, I was hoping for a possibility to filter directly via URL, as the meta_key and meta_value seem to be supported according to the JSON API docs.
    – saschoar
    Commented Feb 17, 2012 at 11:49
  • Sorry, but I really don't know more than what I can read in the API docs. Your best bet with my help is to manually add meta query stuff in. Afaik there's an earlier hook where you should be able to $query->set() the meta keys in.
    – kaiser
    Commented Feb 17, 2012 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.