Tagged: 

Viewing 7 reply threads
  • Author
    Posts
    • #4715
      Rachel
      Member

      Hello 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'
              )
            )
          )
        )
      ));
      
    • #4718
      Rachel
      Member

      Essentially I want to get some awareness in the data model of the user chosen sorting order of the add more fields which are draggable.

      sources

      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.
    • #4723
      Kevin
      Keymaster

      @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

    • #4743
      Rachel
      Member

      Is there a way to expose this in the same array? I’m using this with the JSON API plugin

    • #4744
      Steve
      Keymaster

      @rcantor– You can save the data in one array by setting a field for the entire group.

        'type' => 'group'
        ,'label' => __('Sources')
        ,'field => 'my_sources'
      

      Now all data will be saved in the field, my_sources, as an array.

      Does that help?

    • #4746
      Rachel
      Member

      Hmmm, 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/"
      }
    • #4747
      Steve
      Keymaster

      WordPress stores this data in a serialized array. You can unserialize two ways:
      1) Use the standard WordPress function get_post_meta:
      $sources_group = get_post_meta($post->ID, 'sources_group', true);

      2) Unserialize with the standard PHP function, unserialize.

      Does that make sense?

      • #4750
        Rachel
        Member

        That does, I was just hoping there was some way of adding an index to the data model… any suggestions?

        I am now using the WP-API plugin and see that they come in the correct order, but I’d like to harden things a bit more by having an index if possible.

    • #4767
      Steve
      Keymaster

      @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.

Viewing 7 reply threads
  • You must be logged in to reply to this topic.