Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Use WP_Query
  • Loading branch information
spacedmonkey committed Nov 25, 2021
commit e87b6c5fcb28027e6c2ed6fc214ad62d06c10e5a
23 changes: 14 additions & 9 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,22 @@ function block_core_navigation_get_first_non_empty_navigation() {
// see:
// - https://github.com/WordPress/wordpress-develop/blob/ba943e113d3b31b121f77a2d30aebe14b047c69d/src/wp-includes/nav-menu.php#L613-L619.
// - https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters.
$navigation_posts = get_posts(
array(
'post_type' => 'wp_navigation',
'order' => 'ASC',
'orderby' => 'name',
'posts_per_page' => 1, // only the first post.
's' => '<!-- wp:', // look for block indicators to ensure we only include non-empty Navigations.
)
$parsed_args = array(
'post_type' => 'wp_navigation',
'no_found_rows' => true,
'order' => 'ASC',
'orderby' => 'name',
'posts_per_page' => 20, // Try the first 20 posts
);
return count( $navigation_posts ) ? $navigation_posts[0] : null;

$navigation_posts = new WP_Query( $parsed_args );
foreach ( $navigation_posts->posts as $navigation_post ) {
if ( has_blocks( $navigation_post ) ) {
return $navigation_post;
}
}

return null;
}

/**
Expand Down