4

I would like to get post with an API method using a kind of meta query. I have 2 custom fields "place" and "dayoweek" and I want to retrieve via API call all the post that have some values for this 2 fields. It would more or less like this if you use php

$args = array(
    'numberposts' => -1,
    'post_type' => 'event',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'place',
            'value' => 'Melbourne',

        ),
        array(
            'key' => 'dayoweek',
            'value' => 'saturday',

        )
    )

);

My problem is..how do you do that using the JSON API get posts method? How do you make this call using arrays..

http://somewebiste/API/get_posts/?post_type=event&meta_key=place&meta_value=andorra&cat=1&meta_key=dayoweek&meta_value=saturday&orderby=title&order=ASC 
2
  • 2
    Do you have any sample code we can look at? What have you tried? It's very difficult to understand what you are asking, so please expand the question to be clearer and include some code. Commented Jun 5, 2014 at 22:48
  • FYI, this is not the same API scheduled for inclusion in core, and is for the JSON-API plugin. Commented Jun 8, 2014 at 15:01

2 Answers 2

4

There are two parts to the solution here.

  1. You need to use a JSON API custom controller
  2. In your custom controller, you'll need to decide how to pass the meta_query data structure

Depending on how robust you need this to be, you could use a variety of approaches. Here is the maximalist approach, that will allow any kind of meta_query, encoding the structure as a JSON string.

<?php

// 1. The class name must match the filename (i.e., "foo.php" for JSON_API_Foo_Controller)
// 2. Save this in your themes folder
// 3. Activate your controller under Settings > JSON API

class JSON_API_Example_Controller {

  public function get_posts_by_meta_query() {
    global $json_api;

    if (empty($_GET['meta_query'])) {
      return array(
        'error' => "Specify a 'meta_query' param."
      );
    }

    $query = array(
      'ignore_sticky_posts' => true,
      'numberposts' => -1,
      'post_type' => 'event',
      'meta_query' => json_decode($_GET['meta_query'], true)
    );
    return array(
      'posts' => $json_api->introspector->get_posts($query)
    );
  }
}

?>

So if you want to pass this meta_query as URL-encoded JSON:

$args = array(
  'numberposts' => -1,
  'post_type' => 'event',
  'meta_query' => array(
    'relation' => 'AND',
    array(
      'key' => 'place',
      'value' => 'Melbourne'
    ),
    array(
      'key' => 'dayoweek',
      'value' => 'saturday'
    )
  )
);

Encode it like so, in JavaScript:

var meta_query = encodeURIComponent(JSON.stringify({
  relation: 'AND',
  0: {
    key: 'place',
    value:'Melbourne'
  },
  1: {
    key: 'dayoweek',
    value: 'saturday'
  }
}));

Here's how that would look as a URL query param (a bit unwieldy): /?json=example/get_posts_by_meta_query&meta_query=%7B%220%22%3A%7B%22key%22%3A%22place%22%2C%22value%22%3A%22Melbourne%22%7D%2C%221%22%3A%7B%22key%22%3A%22dayoweek%22%2C%22value%22%3A%22saturday%22%7D%2C%22relation%22%3A%22AND%22%7D

0
0

I find an easy way to do it (for me) by setting the parameters you need on your controller directly on the query.

$query = array(
      'ignore_sticky_posts' => true,
      'numberposts' => -1,
      'post_type' => 'event',

      'meta_query' => array(
        'relation' => 'AND',
        array(
          'key' => 'place',
          'value' => $_GET['place'],

          ),
        array(
          'key' => 'dayoweek',
          'value' => $_GET['dayoweek'],

          )
        ) 
      );

Then the URL looks likes: json=example/get_posts_by_meta_query&place=Toronto&dayoweek=saturday

Sorry If it looks like I'm auto-answering my question, I just do it only in case somebody wants to have another way to do that.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.