WordPress.org

Ready to get started?Download WordPress

Forums

JSON API
FIXED: Get all custom fields the right way! (16 posts)

  1. chris_mcclellan
    Member
    Posted 1 year ago #

    Now you can pass in either { * | true | all } and retrieve all of your custom fields.
    Any of these will work
    ?json=true&custom_fields=*
    ?json=true&custom_fields=true
    ?json=true&custom_fields=all

    There is also a fix for custom fields that are stored as serialized arrays... for us theme developers who do this. :)

    You can replace the function or method set_custom_fields_value(){} with the following...

    You can download the raw version here: http://codepad.org/WZVmodoB

    // Updated <code>set_custom_fields_value()</code> to retreive all fields properly
    	function set_custom_fields_value() {
    
    		global $json_api;
    
    		if ($json_api->include_value('custom_fields') && $json_api->query->custom_fields) {
    
    			// Query string params for this query var
    			$params = trim($json_api->query->custom_fields);
    
    			// Get all custom fields if true|all|* is passed
    			if ($params === "*" || $params === "true" || $params === "all") {
    
    				$wp_custom_fields = get_post_custom($this->id);
    				$this->custom_fields = new stdClass();
    
    				// Loop through our custom fields and place on property
    				foreach($wp_custom_fields as $key => $val) {
    
    					if ($val) {
    
    						// Some fields are stored as serialized arrays.
    						// This method does not support multidimensionals...
    						// but didn't see anything wrong with this approach
    						$current_custom_field = @unserialize($wp_custom_fields[$key][0]);
    
    						if (is_array($current_custom_field)) {
    
    							// Loop through the unserialized array
    							foreach($current_custom_field as $sub_key => $sub_val) {
    
    								// Lets append these for correct JSON output
    								$this->custom_fields->$key->$sub_key = $sub_val;
    							}
    
    						} else {
    
    							// Break this value of this custom field out of its array
    							// and place it on the stack like usual
    							$this->custom_fields->$key = $wp_custom_fields[$key][0];
    
    						}
    					}
    				}
    			} else {
    
    				// Well this is the old way but with the unserialized array fix
    				$params = explode(',', $params);
    				$wp_custom_fields = get_post_custom($this->id);
    				$this->custom_fields = new stdClass();
    
    				foreach ($params as $key) {
    
    					if (isset($wp_custom_fields[$key]) && $wp_custom_fields[$key][0] ) {
    						$current_custom_field = @unserialize($wp_custom_fields[$key][0]);
    
    						if (is_array($current_custom_field)) {
    							foreach($current_custom_field as $sub_key => $sub_val) {
    								$this->custom_fields->$key->$sub_key = $sub_val;
    							}
    
    						} else {
    							$this->custom_fields->$key = $wp_custom_fields[$key][0];
    
    						}
    
    					}
    				}
    			}
    
    		} else {
    			unset($this->custom_fields);
    
    		}
    	}

    I think the author should feel free to include this...

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

  2. chris_mcclellan
    Member
    Posted 1 year ago #

    This is in /models/post.php

  3. Brian
    Member
    Posted 1 year ago #

    Thanks for this Chris.. I was dreading the taunting task of inputting each custom field.

  4. aaayersss
    Member
    Posted 1 year ago #

    Chris - I changed my set_custom_fields_value(){} in the /models/post.php and I'm still not seeing my custom fields - any ideas?

  5. aaayersss
    Member
    Posted 1 year ago #

    I didn't quite understand what this means:

    Now you can pass in either { * | true | all } and retrieve all of your custom fields.
    Any of these will work
    ?json=true&custom_fields=*
    ?json=true&custom_fields=true
    ?json=true&custom_fields=all

    I didn't know where to put this in the original code. So I used your serialized arrays code but I'm not using serialized arrays in my custom fields.

  6. chris_mcclellan
    Member
    Posted 1 year ago #

    This only checks for a serialized array in your custom field but doesn't affect anything if it doesn't exist.

    You should just overwrite the set_custom_fields_value(){ ... }
    with the one above and you should be able to pass custom_fields=all to get all your custom fields

  7. chris_mcclellan
    Member
    Posted 1 year ago #

    There really isn't anything different about the way you get custom_fields from the way it was done originally. You can just retrieve all of your custom fields at one time without having to pass which custom fields you want like the old way.

    OLD WAY:
    http://myurl.com/?json=true&custom_fields=_my_var_1,_my_var_2,_my_var_2

    NEW WAY
    http://myurl.com/?json=true&custom_fields=all

  8. aaayersss
    Member
    Posted 1 year ago #

    Now I get it! It worked! Thanks Chris!

  9. phillipadsmith
    Member
    Posted 1 year ago #

    @chris_mcclellan Any chance you can re-post that snippet somewhere else, as it appears that codepad.org is down.

    I'm getting an error with this line:

    $this->custom_fields->$key->$sub_key = $sub_val;

    So I'm curious to see if you've made an update to address that.

  10. chris_mcclellan
    Member
    Posted 1 year ago #

    I'm using this one now which just adds the custom fields every time - this way its less string manipulation I have to perform at any given point. This works for me... and does not address that line you posted.

    // Updated <code>set_custom_fields_value()</code> to retreive all fields properly
    	function set_custom_fields_value() {
    
    		global $json_api;
    
    		// Query string params for this query var
    		$params = trim($json_api->query->custom_fields);
    		$wp_custom_fields = get_post_custom($this->id);
    		$this->custom_fields = new stdClass();
    
    		// Loop through our custom fields and place on property
    		foreach($wp_custom_fields as $key => $val) {
    
    			if ( $val ) {
    
    				// Some fields are stored as serialized arrays.
    				// This method is not recursive. Does not support multidimensional arrays.
    				$current_custom_field = @unserialize($val[0]);
    
    				if (is_array($current_custom_field)) {
    
    					// Loop through the unserialized array
    					foreach($current_custom_field as $sub_key => $sub_val) {
    
    						// Lets append these for correct JSON output
    						$this->custom_fields->$key->$sub_key = $sub_val;
    					}
    
    				} else {
    
    					// Break this value of this custom field out of its array
    					// and place it on the json blob like usual
    					$this->custom_fields->$key = $val[0];
    
    				}
    			}
    		}
    
    	}
  11. phillipadsmith
    Member
    Posted 1 year ago #

    @chris_mcclellan: Hmmmm, I'm getting this error from that code:

    Warning: Creating default object from empty value in /wp-content/plugins/json-api/models/post.php on line 286

    ... which is:

    $this->custom_fields->$key->$sub_key = $sub_val;

    Thoughts?

  12. chris_mcclellan
    Member
    Posted 1 year ago #

    You could try... Sorry i'm at work so I can't test it.

    $this->custom_fields->$key[$sub_key] = $sub_val;

  13. phillipadsmith
    Member
    Posted 1 year ago #

    I don't get an error on get_recent_posts, but when I try to also list custom post types, I'mg getting the error (still).

  14. chris_mcclellan
    Member
    Posted 1 year ago #

    hmmm. I'll give it a look when I get home.
    It should work as there are only minor changes on the original code base.

  15. chris_mcclellan
    Member
    Posted 1 year ago #

    FIXED. Now this returns the array correctly... ugh that should have been the solution from the beginning.

    ENJOY!

    // Updated <code>set_custom_fields_value()</code> to retreive all fields properly
    	function set_custom_fields_value() {
    
    		global $json_api;
    
    		// Query string params for this query var
    		$params = trim($json_api->query->custom_fields);
    		$wp_custom_fields = get_post_custom($this->id);
    		$this->custom_fields = new stdClass();
    
    		// Loop through our custom fields and place on property
    		foreach($wp_custom_fields as $key => $val) {
    
    			if ( $val ) {
    
    				// Some fields are stored as serialized arrays.
    				// This method is not recursive. Does not support multidimensional arrays.
    				$current_custom_field = @unserialize($val[0]);
    
    				if (is_array($current_custom_field)) {
    
    					// This item is an array - lets append it as such
    					$this->custom_fields->$key = $current_custom_field;
    
    				} else {
    
    					// Break this value of this custom field out of its array
    					// and place it on the json blob like usual
    					$this->custom_fields->$key = $val[0];
    
    				}
    			}
    		}
    	}
  16. Marioario
    Member
    Posted 1 year ago #

    Hey Chris, great plugin. Thanks for the custom function up there.

    Any chance you'll merge this into a new release soon?

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic