• Resolved jaredb138

    (@jaredb138)


    Do you plan to add support to allow filtering of results generated from queries like /wp/v2/posts?categories=1 so that any posts returned can be filtered? If I try that now I get the following error (likely because the built-in API is trying to parse the ?fields=... ):

    {
      "code": "rest_invalid_param",
      "message": "Invalid parameter(s): categories",
      "data": {
        "status": 400,
        "params": {
          "categories": "categories[0] is not of type integer."
        }
      }
    }

    Edit: Figured it out. This plugin does filter out search results. I had a syntax error and was doing /wp/v2/posts?categories=1?fields=id instead of /wp/v2/posts?categories=1&fields=id Great Plugin, thanks again.

    • This topic was modified 7 years, 5 months ago by jaredb138. Reason: Issue was an error on my part
    • This topic was modified 7 years, 5 months ago by jaredb138.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author svrooij

    (@svrooij)

    @jaredb138 I’m glad you found this out yourself! Could you maybe provide me a better way on how this would be documented?

    This plugin is made in some spare time, and if you use this plugin in some production website/app, it would be great if you could support it a little. Buy me a beer ??

    Thread Starter jaredb138

    (@jaredb138)

    Sure I would add this:

    The REST API – Filter Fields plugin can also filter the results generated from a native WP API filter (or search). For instance, doing /wp-json/wp/v2/posts?categories=34 will return the last 10 posts from the category with the ID of 34.

    You can use the Filter Fields plugin with queries like this by appending &fields=<field-name> to the end of that query like /wp-json/wp/v2/posts?categories=34&fields=<field-name>

    Using /wp-json/wp/v2/posts?categories=34&fields=id will return only the id’s of the 10 posts returned by the native category filter.

    The Filter Fields plugin can also filter the fields of API searches. Doing /wp-json/wp/v2/posts?search=foo&fields=title,content.rendered (foo being the search term) will return the title and post content of all posts that contain the search term “foo”.

    It is important to note that the order of parameter queries is not important. You can call the native filter query in the URL first, or you can call the Filter Fields Plugin first.

    Simply append the other query with an ampersand. For example:

    Using
    /wp-json/wp/v2/posts?search=foo&fields=title,content.rendered

    and

    Using
    /wp-json/wp/v2/posts?fields=title,content.rendered&search=foo

    will return the exact same results.

    • This reply was modified 7 years, 5 months ago by jaredb138. Reason: formatting
    Plugin Author svrooij

    (@svrooij)

    Thanks for the help, this will be in the readme when I update the plugin!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Any Support for filtering search queries in the future? [Edit: It already does]’ is closed to new replies.