diff --git a/src/wp-admin/includes/nav-menu.php b/src/wp-admin/includes/nav-menu.php index f07b25e36c..b65e28ce98 100644 --- a/src/wp-admin/includes/nav-menu.php +++ b/src/wp-admin/includes/nav-menu.php @@ -70,24 +70,28 @@ function _wp_ajax_menu_quick_search( $request = array() ) { } elseif ( preg_match('/quick-search-(posttype|taxonomy)-([a-zA-Z_-]*\b)/', $type, $matches) ) { if ( 'posttype' == $matches[1] && get_post_type_object( $matches[2] ) ) { - query_posts(array( - 'posts_per_page' => 10, - 'post_type' => $matches[2], - 's' => $query, - )); - if ( ! have_posts() ) + $search_results_query = new WP_Query( array( + 'no_found_rows' => true, + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'posts_per_page' => 10, + 'post_type' => $matches[2], + 's' => $query, + ) ); + if ( ! $search_results_query->have_posts() ) { return; - while ( have_posts() ) { - the_post(); + } + while ( $search_results_query->have_posts() ) { + $post = $search_results_query->next_post(); if ( 'markup' == $response_format ) { - $var_by_ref = get_the_ID(); + $var_by_ref = $post->ID; echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_post( $var_by_ref ) ) ), 0, (object) $args ); } elseif ( 'json' == $response_format ) { echo wp_json_encode( array( - 'ID' => get_the_ID(), - 'post_title' => get_the_title(), - 'post_type' => get_post_type(), + 'ID' => $post->ID, + 'post_title' => get_the_title( $post->ID ), + 'post_type' => $matches[2], ) ); echo "\n"; diff --git a/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php b/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php new file mode 100644 index 0000000000..54fa9cfdb6 --- /dev/null +++ b/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php @@ -0,0 +1,29 @@ +post->create_many( 3, array( 'post_type' => 'page', 'post_content' => 'foo' ) ); + self::factory()->post->create( array( 'post_type' => 'page', 'post_content' => 'bar' ) ); + + $request = array( + 'type' => 'quick-search-posttype-page', + 'q' => 'foo', + 'response-format' => 'json', + ); + + $output = get_echo( '_wp_ajax_menu_quick_search', array( $request ) ); + $this->assertNotEmpty( $output ); + + $results = explode( "\n", trim( $output ) ); + $this->assertCount( 3, $results ); + } +}