WordPress.org

Make WordPress Core

Opened 8 months ago

Last modified 4 weeks ago

#46264 new defect (bug)

Page attributes 'ORDER' does not saving data for post-type 'post'.

Reported by: tanvirul Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.0
Component: Editor Keywords: has-patch
Focuses: rest-api Cc:
PR Number:

Description

I used add_post_type_support() function to support 'page-attributes' for Menu Order at post-type 'post'.

<?php
function prefix_add_posts_order() {
        add_post_type_support( 'post', 'page-attributes' );
}
add_action( 'admin_init', 'prefix_add_posts_order' );

When I save the post after putting an integer value in the Order field then no value is saving on 'wp_posts' table 'menu_order' column and it does not also update on the editor page.

But when I switch to TinyMCE editor it's working fine.

Attachments (2)

46264.diff (1.0 KB) - added by felipeelia 7 months ago.
46264.2.diff (2.0 KB) - added by websupporter 4 weeks ago.
unit test added

Download all attachments as: .zip

Change History (6)

@felipeelia
7 months ago

#1 @felipeelia
7 months ago

  • Keywords has-patch needs-unit-tests added; needs-patch removed

Hi @tanvirul and welcome to WordPress trac :)

You shouldn't hook your function to admin_init, but to init. Anyway, I was able to reproduce the problem you reported.

That seems to happen because, for posts, pages, and attachments, the JSON Schema generated by WP_REST_Posts_Controller::get_item_schema() is restricted by the $fixed_schemas variable. That said, the menu_order field is never exposed to the Rest API.

I've uploaded a patch that uses $fixed_schemas for default attributes, but still checks if those post types support other attributes. Perhaps we should remove $fixed_schemas at all, and always check this dynamically, but I guess it was there for a reason, so let's wait for somebody else input here.

Also, for the Tests suite, I think we should probably move/copy test_update_page_menu_order_* functions from WP_Test_REST_Pages_Controller to use them for all post types (custom or not). But, again, someone more experienced may have better input for this.

#2 @lordspace
4 months ago

menu_order doesn't seem to retain its value in Gutenberg.
WP v5.2.2

@websupporter
4 weeks ago

unit test added

#3 @websupporter
4 weeks ago

  • Focuses rest-api added
  • Keywords needs-unit-tests removed

Hi,
I just stumbled upon the same problem. Thank you for reporting @tanvirul!

I think the general idea of the fixed schema is the following: If you would generate those keys (menu_order, excerpt, etc) only dynamically, a lot of clients might break, as they might just assume every post object has an excerpt. The fixed schema ensures some stability for the core features the content types have.

That said, it is still possible to extend the basic data concept (register_rest_field()), so the fixed schema does not mean its intended to serve only those data values.

So while remove_post_type_support() should not remove properties from the JSON output, I think add_post_type_support() should add properties to it.

I tried @felipeelia fix and it works as intended. Thank you for providing it. My last patch added a unit test for the intended behavior.

In the meantime, this workaround helped me:

<?php
add_action(
    'rest_api_init',
    function() {
        register_rest_field(
            'post',
            'menu_order',
            [
                'get_callback' => function($object) {
                    if (! isset($object['menu_order'])) {
                        return 0;
                    }
                    return (int) $object['menu_order'];
                },
                'schema' => [
                    'type' => 'integer',
                ]
            ]
        );
    }
);
Last edited 4 weeks ago by websupporter (previous) (diff)

This ticket was mentioned in Slack in #core-restapi by websupporter. View the logs.


4 weeks ago

Note: See TracTickets for help on using tickets.