diff --git a/lib/compat.php b/lib/compat.php index 83b09c976ebe4..2a592d1683a8b 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -472,3 +472,24 @@ function gutenberg_render_block_with_assigned_block_context( $pre_render, $parse * @see WP_Block::render */ remove_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' ); + +/** + * Filters a REST query ordered by menu_order to ensure a repeatable sequence over multiple pages. + * + * This can be removed when WordPress core supports pagination of requests ordering on menu_order. + * + * @see https://core.trac.wordpress.org/ticket/46294 + * + * @param string $orderby Current orderby value. + * @param WP_Query $query Query object. + */ +function gutenberg_posts_orderby( $orderby, $query ) { + global $wpdb; + if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { + if ( 'menu_order' === $query->query['orderby'] ) { + $orderby = "$wpdb->posts.menu_order,$wpdb->posts.post_title,$wpdb->posts.id " . $query->query['order']; + } + } + return $orderby; +} +add_filter( 'posts_orderby', 'gutenberg_posts_orderby', 10, 2 );