From 60bc9b6a3b1d14cf40eda4fe7e6a88e535ae90a6 Mon Sep 17 00:00:00 2001 From: Herb Miller Date: Thu, 2 Jul 2020 10:50:14 +0100 Subject: [PATCH] Issue #12795 - alter orderby for menu_order to ensure unique, reproducable ordering --- lib/compat.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/compat.php b/lib/compat.php index 83b09c976ebe47..68dbf7715f5fa4 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 ( $query->query['orderby'] === 'menu_order' ) { + $orderby = "$wpdb->posts.menu_order,post_title,id asc"; + } + } + return $orderby; +} +add_filter( 'posts_orderby', 'gutenberg_posts_orderby', 10, 2 );