Make WordPress Core

Changeset 65918


Ignore:
Timestamp:
12/04/2026 01:07:58 PM (5 hours ago)
Author:
wildworks
Message:

REST API: Add excerpt_length to post collection endpoints.

Registers the excerpt_length parameter on post collection endpoints, for post types that support excerpts, with the same definition as the single post endpoint. Requests that list several posts can now ask for an excerpt at a given length through a documented and validated parameter, without filtering the excerpt length globally.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/16511

Follow-up to [58065].

Props swissspidy, wildworks.
Fixes #66940.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r65581 r65918  
    30173017         * @since 5.4.0 The `tax_relation` query parameter was added.
    30183018         * @since 5.7.0 The `modified_after` and `modified_before` query parameters were added.
     3019         * @since 7.3.0 The `excerpt_length` query parameter was added.
    30193020         *
    30203021         * @return array Collection parameters.
    … …  
    32073208                                        'type' => 'string',
    32083209                                ),
     3210                        );
     3211                }
     3212
     3213                $schema = $this->get_item_schema();
     3214                if ( isset( $schema['properties']['excerpt'] ) ) {
     3215                        $query_params['excerpt_length'] = array(
     3216                                'description' => __( 'Override the default excerpt length.' ),
     3217                                'type'        => 'integer',
    32093218                        );
    32103219                }
  • trunk/tests/phpunit/tests/rest-api/rest-pages-controller.php

    r64596 r65918  
    7979                                'before',
    8080                                'context',
     81                                'excerpt_length',
    8182                                'exclude',
    8283                                'include',
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r65757 r65918  
    201201                                'categories_exclude',
    202202                                'context',
     203                                'excerpt_length',
    203204                                'exclude',
    204205                                'format',
    … …  
    27502751                );
    27512752                $this->assertSame( 0, $filter_count );
     2753        }
     2754
     2755        /**
     2756         * @ticket 66940
     2757         *
     2758         * @covers WP_REST_Posts_Controller::get_items
     2759         */
     2760        public function test_get_items_override_excerpt_length() {
     2761                $post_id = self::factory()->post->create(
     2762                        array(
     2763                                'post_excerpt' => '',
     2764                                'post_content' => implode( ' ', array_fill( 0, 60, 'word' ) ),
     2765                        )
     2766                );
     2767
     2768                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     2769                $request->set_param( 'include', array( $post_id ) );
     2770                $request->set_param( 'excerpt_length', 10 );
     2771                $response = rest_get_server()->dispatch( $request );
     2772                $data     = $response->get_data();
     2773
     2774                $this->assertSame( 200, $response->get_status(), 'The request should succeed.' );
     2775                $this->assertCount( 1, $data, 'Exactly one post should be returned.' );
     2776
     2777                // 10 words plus the ellipsis added via the 'excerpt_more' filter.
     2778                $this->assertCount(
     2779                        11,
     2780                        explode( ' ', trim( wp_strip_all_tags( $data[0]['excerpt']['rendered'] ) ) ),
     2781                        'Expected the excerpt to contain 11 words (10 words plus an ellipsis).'
     2782                );
     2783        }
     2784
     2785        /**
     2786         * @ticket 66940
     2787         *
     2788         * @covers WP_REST_Posts_Controller::get_items
     2789         */
     2790        public function test_get_items_invalid_excerpt_length() {
     2791                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     2792                $request->set_param( 'excerpt_length', 'abc' );
     2793                $response = rest_get_server()->dispatch( $request );
     2794
     2795                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    27522796        }
    27532797
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r65828 r65918  
    658658                            },
    659659                            "required": false
     660                        },
     661                        "excerpt_length": {
     662                            "description": "Override the default excerpt length.",
     663                            "type": "integer",
     664                            "required": false
    660665                        }
    661666                    }
    … …  
    18631868                                "type": "string"
    18641869                            },
     1870                            "required": false
     1871                        },
     1872                        "excerpt_length": {
     1873                            "description": "Override the default excerpt length.",
     1874                            "type": "integer",
    18651875                            "required": false
    18661876                        }
    … …  
    48904900                            ],
    48914901                            "required": false
     4902                        },
     4903                        "excerpt_length": {
     4904                            "description": "Override the default excerpt length.",
     4905                            "type": "integer",
     4906                            "required": false
    48924907                        }
    48934908                    }
Note: See TracChangeset for help on using the changeset viewer.