Skip to content
Merged
Changes from all commits
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
24 changes: 15 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,23 @@ 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',
'post_status' => 'publish',
'posts_per_page' => 20, // Try the first 20 posts.
Copy link
Contributor

@adamziel adamziel Nov 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to filter by post_content != ''. Is there a need to fetch more than 1? Do we ever expect to see a non-empty wp_navigation post that has no blocks in it? CC @getdave @noisysocks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't expect it but I guess it could happen...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

);
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