Skip to content

Commit

Permalink
REST API: Add support for the 'ignore_sticky_posts' argument (WordPre…
Browse files Browse the repository at this point in the history
…ss#68970)


Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: spacedmonkey <spacedmonkey@git.wordpress.org>
Co-authored-by: TimothyBJacobs <timothyblynjacobs@git.wordpress.org>
Co-authored-by: iamtakashi <iamtakashi@git.wordpress.org>
  • Loading branch information
5 people authored Feb 4, 2025
1 parent 06379a3 commit 2670a56
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
3 changes: 3 additions & 0 deletions backport-changelog/6.8/8228.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/8228

* https://github.com/WordPress/gutenberg/pull/68970
46 changes: 44 additions & 2 deletions lib/compat/wordpress-6.8/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ function gutenberg_add_default_template_part_areas_to_index( WP_REST_Response $r
$response->data['default_template_part_areas'] = get_allowed_block_template_part_areas();
return $response;
}

add_filter( 'rest_index', 'gutenberg_add_default_template_part_areas_to_index' );

/**
Expand All @@ -70,5 +69,48 @@ function gutenberg_add_default_template_types_to_index( WP_REST_Response $respon
$response->data['default_template_types'] = $indexed_template_types;
return $response;
}

add_filter( 'rest_index', 'gutenberg_add_default_template_types_to_index' );

/**
* Adds `ignore_sticky` parameter to the post collection endpoint.
*
* Note: Backports into the wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php file.
*
* @param array $query_params JSON Schema-formatted collection parameters.
* @param WP_Post_Type $post_type Post type object.
* @return array
*/
function gutenberg_modify_post_collection_paramt( $query_params, WP_Post_Type $post_type ) {
if ( 'post' === $post_type->name && ! isset( $query_params['ignore_sticky'] ) ) {
$query_params['ignore_sticky'] = array(
'description' => __( 'Whether to ignore sticky posts or not.' ),
'type' => 'boolean',
'default' => false,
);
}

return $query_params;
}
add_filter( 'rest_post_collection_params', 'gutenberg_modify_post_collection_paramt', 10, 2 );

/**
* Modify posts query based on `ignore_sticky` parameter.
*
* Note: Backports into the wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php file.
*
* @param array $prepared_args Array of arguments for WP_User_Query.
* @param WP_REST_Request $request The REST API request.
* @return array Modified arguments
*/
function gutenberg_modify_post_collection_query( $args, WP_REST_Request $request ) {
/*
* Honor the original REST API `post__in` behavior. Don't prepend sticky posts
* when `post__in` has been specified.
*/
if ( isset( $request['ignore_sticky'] ) && empty( $args['post__in'] ) ) {
$args['ignore_sticky_posts'] = $request['ignore_sticky'];
}

return $args;
}
add_filter( 'rest_post_query', 'gutenberg_modify_post_collection_query', 10, 2 );

0 comments on commit 2670a56

Please sign in to comment.