• wprocks

    (@wordpressrocks)


    hi all,
    firstly great to be here and I love this JSON API plugin!

    I have a short question though, and hoping someone can help me out:

    In order to create a new post we do this:
    http://localhost/public_html/website/?json=posts/create_post&dev=1&nonce=eaa7969ea5&categories=competitions,photos&title=hello%20world%204&content="mycontent"&custom_fields=1&status=publish

    By setting custom_fields=1 , we can get the custom field object in the Post Response Object.

    I was wondering how can we create a post, with a specific value for its custom field?

    Take for instance, i have a custom_field and the key is competition.
    And I want to give it a value “summer”

    So perhaps i can do something like this:

    http://localhost/public_html/website/?json=posts/create_post&dev=1&nonce=eaa7969ea5&categories=competitions,photos&title=hello%20world%204&content="mycontent"&custom_fields=competition:summer&status=publish
    But the above request will not work, since this is not supported?

    I was wondering how can i creat a post with a specific value forits custom field ?

    Thanks in advance!

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

Viewing 3 replies - 1 through 3 (of 3 total)
  • in the post.php file there is the function save($values = null). simply add the following code to the end of this file

    foreach($values as $key => $value){
    if(strlen(strstr($key,’id’))==0 &&
    strlen(strstr($key,’type’))==0 &&
    strlen(strstr($key,’status’))==0 &&
    strlen(strstr($key,’title’))==0 &&
    strlen(strstr($key,’content’))==0 &&
    strlen(strstr($key,’author’))==0 &&
    strlen(strstr($key,’categories’))==0 &&
    strlen(strstr($key,’tags’))==0)//it must be a custom_field, so add it
    update_post_meta($this->id,$key,$value);
    }

    $wp_post = get_post($this->id);
    $this->import_wp_object($wp_post);

    return $this->id;
    }

    after that you should be able to create a post with custom values.
    According to your example simply call
    http://localhost/public_html/website/?json=posts/create_post&dev=1&nonce=eaa7969ea5&categories=competitions,photos&title=hello%20world%204&content=”mycontent”&competition=summer&status=publish

    to add the value “summer” to your field competition

    Amazing! This is exactly what I needed thanks cohen!

    Hello.

    I solved this by adding the following in post.php in the save function:

    // add custom fields
      if ( !empty($values["custom"]) ) {
        	foreach ($values["custom"] as $metakey => $metavalue) {
        		update_post_meta($this->id,$metakey,$metavalue);
        	}
     }

    The query:

    http://localhost/site/?json=posts/create_post&dev=1&nonce=xxx&title=Post%20title&status=publish&custom[custom_field_1]=value1&custom[custom_field_2]=value2

    Query for listing the fields in the response:

    http://localhost/site/?json=posts/create_post&dev=1&nonce=xxx&title=Post%20title&status=publish&custom[custom_field_1]=value1&custom[custom_field_2]=value2&custom_fields=custom_field_1,custom_field_2

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Plugin: JSON API] Creating post with a value for its custom_field’ is closed to new replies.