wpseek.com
A WordPress-centric search engine for devs and theme authors



block_core_navigation_get_first_non_empty_navigation › WordPress Function

Sincen/a
Deprecatedn/a
block_core_navigation_get_first_non_empty_navigation ( No parameters )
Returns:
  • (WP_Post|null) the first non-empty Navigation or null.
Defined at:
Codex:

Finds the first non-empty `wp_navigation` Post.



Source

function block_core_navigation_get_first_non_empty_navigation() {
	// Order and orderby args set to mirror those in `wp_get_nav_menus`
	// 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.
	$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.
	);

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

	return null;
}