• Anonymous User 14388610

    (@anonymized-14388610)


    So I don’t know how to properly word this because I am only starting out learning Gutenberg but I am having trouble with the attributes that are passed from the Block Editor to PHP whenever more than one category is selected. This can best be viewed in the core/latest-posts block.

    When adding more than one category to the “categories” field, the attributes that are passed along with the first element just vanish, maybe you can see it better in this example:

    [
      {
        "id": 14,
        "value": "Beitragskat"
      },
      {
        "id": 1,
        "count": 12,
        "description": "",
        "link": "http://localhost/wordpress/index.php/category/allgemein/",
        "name": "Allgemein",
        "slug": "allgemein",
        "taxonomy": "category",
        "parent": 0,
        "meta": [],
        "_links": {
          "self": [
            {
              "href": "http://localhost/wordpress/index.php/wp-json/wp/v2/categories/1"
            }
          ],
          "collection": [
            {
              "href": "http://localhost/wordpress/index.php/wp-json/wp/v2/categories"
            }
          ],
          "about": [
            {
              "href": "http://localhost/wordpress/index.php/wp-json/wp/v2/taxonomies/category"
            }
          ],
          "wp:post_type": [
            {
              "href": "http://localhost/wordpress/index.php/wp-json/wp/v2/posts?categories=1"
            }
          ],
          "curies": [
            {
              "name": "wp",
              "href": "https://api.w.org/{rel}",
              "templated": true
            }
          ]
        }
      }
    ]

    Here you can see, that the category with the id ’14’ now does not have all this extra information that the category with the id ‘1’ has. Also, when category ‘1’is deleted and only category ’14’ remains, after saving the attributes[‘categories’] look like this:

    [
      {
        "id": 14,
        "value": "Beitragskat"
      }
    ]
    

    When removing category ’14’ and re-adding it, the object now looks like this:

    [
      {
        "id": 14,
        "count": 1,
        "description": "",
        "link": "http://localhost/wordpress/index.php/category/beitragskat/",
        "name": "Beitragskat",
        "slug": "beitragskat",
        "taxonomy": "category",
        "parent": 0,
        "meta": [],
        "_links": {
          "self": [
            {
              "href": "http://localhost/wordpress/index.php/wp-json/wp/v2/categories/14"
            }
          ],
          "collection": [
            {
              "href": "http://localhost/wordpress/index.php/wp-json/wp/v2/categories"
            }
          ],
          "about": [
            {
              "href": "http://localhost/wordpress/index.php/wp-json/wp/v2/taxonomies/category"
            }
          ],
          "wp:post_type": [
            {
              "href": "http://localhost/wordpress/index.php/wp-json/wp/v2/posts?categories=14"
            }
          ],
          "curies": [
            {
              "name": "wp",
              "href": "https://api.w.org/{rel}",
              "templated": true
            }
          ]
        }
      }
    ]

    This is not a problem in this block in particular, but I am trying to add a similar block based on a custom taxonomy and not being able to access all the other properties all the time comes in the way.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator threadi

    (@threadi)

    Where do you see these JSON arrays? When I select a category in latest posts, an AJAX request for saving is sent out containing the generated Gutenberg code.

    What are you specifically concerned with here? You want to build a block yourself and use the categories in it similar to Latest Posts? For this you first need the combo box component: https://developer.wordpress.org/block-editor/reference-guides/components/combobox-control/ – there you have to enter all currently available categories in options. You can query them again when loading the block:

    wp.data.select('core').getEntityRecords('taxonomy', 'category');

    See: https://wordpress.stackexchange.com/questions/319035/how-would-i-get-a-taxonomy-category-list-inside-a-gutenberg-block

    Thread Starter Anonymous User 14388610

    (@anonymized-14388610)

    Hey, thanks for the reply.

    those JSON arrays are just regular PHP arrays converted to JSON objects for better visibility, that’s what my IDE could do itself. Just imagine it to be a PHP array

    I am using the QueryControls, just like the core block. Unfortunately, combobox control would not be sufficient for this task since it is only made to select one category. I encourage you to set a breakpoint at the render_block_core_latest_posts() function and watch the $attributes[‘categories’] to see what I mean. If just one category is added, the full set of attributes is passed but once another category is added the information from the first category vanishes.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Gutenberg: attributes for category not always containing slug / taxonomy’ is closed to new replies.