Tagged: json api
- This topic has 8 replies, 3 voices, and was last updated 6 years, 3 months ago by
Steve.
-
AuthorPosts
-
-
October 23, 2015 at 1:37 pm #4715
RachelMemberHello piklist friends,
I was wondering if anyone would know how to number the order of the drag and drop fields of an add_more group?
I would like to eventually be able to have citations below a post, which are sortable, and labeled by their order in the editor view.
piklist('field', array( 'type' => 'group' ,'label' => __('Sources') ,'description' => __('Any additional sources to cite') ,'field' => 'sources_group' ,'add_more' => true ,'fields' => array( array( 'type' => 'text' ,'field' => 'source_title' ,'label' => 'Source Title' ,'columns' => 12 ,'attributes' => array( 'class' => 'large-text' ) ) ,array( 'type' => 'text' ,'field' => 'source_text' ,'label' => 'Source Text' ,'columns' => 12 ,'attributes' => array( 'class' => 'large-text' ) ) ,array( 'type' => 'text' ,'field' => 'source_url' ,'label' => 'Source URL' ,'columns' => 12 ,'attributes' => array( 'class' => 'large-text' ), 'validate' => array( array( 'type' => 'url' ) ) ) ) )); -
October 23, 2015 at 4:26 pm #4718
RachelMemberEssentially I want to get some awareness in the data model of the user chosen sorting order of the add more fields which are draggable.

Perhaps the json would look something like this:
{ "sources_group": { "order": "0", "source_title": "Title of source", "source_text": "some text", "source_url": "http://www.somewhere.com" }, "order": "1", "source_title": "Title of source", "source_text": "more text", "source_url": "http://www.somewhereelse.com" } }Attachments:
You must be logged in to view attached files. -
October 23, 2015 at 7:19 pm #4723
KevinKeymaster@rachel-
The data in the add-more fields is stored in that order in the database. So in the case of your post meta field, the data is ordered by meta_id, the lowest id being the first item in the add-more field. Let me know if you have any more questions.
Thanks,
Kevin
-
October 26, 2015 at 10:25 am #4743
RachelMemberIs there a way to expose this in the same array? I’m using this with the JSON API plugin
-
October 26, 2015 at 10:43 am #4744
-
October 26, 2015 at 11:00 am #4746
RachelMemberHmmm, there appears to be an a and s key in the array, but it seems to not have anything to do with source order.
Here’s my piklist code:
piklist('field', array( 'type' => 'group' ,'label' => __('Sources') ,'description' => __('Any additional sources to cite') ,'field' => 'sources_group' ,'add_more' => true ,'fields' => array( array( 'type' => 'text' ,'field' => 'source_title' ,'label' => 'Source Title' ,'columns' => 12 ,'attributes' => array( 'class' => 'large-text' ) ) ,array( 'type' => 'text' ,'field' => 'source_text' ,'label' => 'Source Text' ,'columns' => 12 ,'attributes' => array( 'class' => 'large-text' ) ) ,array( 'type' => 'text' ,'field' => 'source_url' ,'label' => 'Source URL' ,'columns' => 12 ,'attributes' => array( 'class' => 'large-text' ), 'validate' => array( array( 'type' => 'url' ) ) ) ) ));and the JSON:
{ "status": "ok", "post": { "id": 31, "type": "post", "slug": "testing-a-cited-post", "url": "http://localhost:8888/2015/10/26/testing-a-cited-post/", "status": "publish", "title": "Testing a cited post", "title_plain": "Testing a cited post", "content": "<p>Body text</p>\n", "excerpt": "<p>Hello excerpt here</p>\n", "date": "2015-10-26 14:58:10", "modified": "2015-10-26 14:58:19", "categories": [ { "id": 2, "slug": "memos", "title": "Memos", "description": "", "parent": 0, "post_count": 2 } ], "tags": [], "author": { "id": 1, "slug": "admin", "name": "admin", "first_name": "", "last_name": "", "nickname": "admin", "url": "", "description": "" }, "comments": [], "attachments": [], "comment_count": 0, "comment_status": "open", "custom_fields": { "media_label": [ "" ], "author_name": [ "" ], "author_title": [ "" ], "author_twitter": [ "" ], "demo_add_more": [ "single" ], "sources_group": [ "a:3:{s:12:\"source_title\";a:2:{i:0;s:25:\"Title of source somewhere\";i:1;s:30:\"Title of Source somewhere else\";}s:11:\"source_text\";a:2:{i:0;s:9:\"more text\";i:1;s:9:\"some text\";}s:10:\"source_url\";a:2:{i:0;s:24:\"http://www.somewhere.com\";i:1;s:28:\"http://www.somewhereelse.com\";}}" ] } }, "previous_url": "http://localhost:8888/2015/10/14/another-post/" } -
October 26, 2015 at 11:09 am #4747
SteveKeymasterWordPress stores this data in a serialized array. You can unserialize two ways:
1) Use the standard WordPress functionget_post_meta:
$sources_group = get_post_meta($post->ID, 'sources_group', true);2) Unserialize with the standard PHP function,
unserialize.Does that make sense?
-
October 26, 2015 at 4:15 pm #4750
-
-
October 27, 2015 at 5:00 pm #4767
SteveKeymaster@rcantor– The json api has a filter, json_api_encode, that you can use to manipulate the data before the output.
Try this:
add_filter('json_api_encode', 'my_json_api_encode'); function my_json_api_encode($data) { // Are we on a single post if (is_single()) { // Get the sources_group field $sources_group = get_metadata('post', $data['post']->id, 'sources_group', true); if ($sources_group) { foreach ($sources_group as $index => &$group) { // Add the index to the objecdt $group['order'] = $index; } // Add updated object back to the json $data['post']->sources_group = $sources_group; } } return $data; }It should render like this:
"sources_group":[ { "source_title":"title 1" ,"source_text":"text 1" ,"source_url":"http:\/\/piklist.com" ,"order":0 } , { "source_title":"title 2" ,"source_text":"text 2" ,"source_url":"http:\/\/google.com" ,"order":1 } ]Let me know if that works for you.
-
-
AuthorPosts
- You must be logged in to reply to this topic.