WP_Customize_Nav_Menus::search_available_items_query( array $args = array() ): array

Performs post queries for available-item searching.

Description

Based on WP_Editor::wp_link_query().

Parameters

$argsarrayoptional
Accepts 'pagenum' and 's' (search) arguments.

Default:array()

Return

array Menu items.

Source

public function search_available_items_query( $args = array() ) {
	$items = array();

	$post_type_objects = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
	$query             = array(
		'post_type'              => array_keys( $post_type_objects ),
		'suppress_filters'       => true,
		'update_post_term_cache' => false,
		'update_post_meta_cache' => false,
		'post_status'            => 'publish',
		'posts_per_page'         => 20,
	);

	$args['pagenum'] = isset( $args['pagenum'] ) ? absint( $args['pagenum'] ) : 1;
	$query['offset'] = $args['pagenum'] > 1 ? $query['posts_per_page'] * ( $args['pagenum'] - 1 ) : 0;

	if ( isset( $args['s'] ) ) {
		$query['s'] = $args['s'];
	}

	$posts = array();

	// Prepend list of posts with nav_menus_created_posts search results on first page.
	$nav_menus_created_posts_setting = $this->manager->get_setting( 'nav_menus_created_posts' );
	if ( 1 === $args['pagenum'] && $nav_menus_created_posts_setting && count( $nav_menus_created_posts_setting->value() ) > 0 ) {
		$stub_post_query = new WP_Query(
			array_merge(
				$query,
				array(
					'post_status'    => 'auto-draft',
					'post__in'       => $nav_menus_created_posts_setting->value(),
					'posts_per_page' => -1,
				)
			)
		);
		$posts           = array_merge( $posts, $stub_post_query->posts );
	}

	// Query posts.
	$get_posts = new WP_Query( $query );
	$posts     = array_merge( $posts, $get_posts->posts );

	// Create items for posts.
	foreach ( $posts as $post ) {
		$post_title = $post->post_title;
		if ( '' === $post_title ) {
			/* translators: %d: ID of a post. */
			$post_title = sprintf( __( '#%d (no title)' ), $post->ID );
		}

		$post_type_label = $post_type_objects[ $post->post_type ]->labels->singular_name;
		$post_states     = get_post_states( $post );
		if ( ! empty( $post_states ) ) {
			$post_type_label = implode( ',', $post_states );
		}

		$items[] = array(
			'id'         => 'post-' . $post->ID,
			'title'      => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),
			'type'       => 'post_type',
			'type_label' => $post_type_label,
			'object'     => $post->post_type,
			'object_id'  => (int) $post->ID,
			'url'        => get_permalink( (int) $post->ID ),
		);
	}

	// Query taxonomy terms.
	$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'names' );
	$terms      = get_terms(
		array(
			'taxonomies' => $taxonomies,
			'name__like' => $args['s'],
			'number'     => 20,
			'hide_empty' => false,
			'offset'     => 20 * ( $args['pagenum'] - 1 ),
		)
	);

	// Check if any taxonomies were found.
	if ( ! empty( $terms ) ) {
		foreach ( $terms as $term ) {
			$items[] = array(
				'id'         => 'term-' . $term->term_id,
				'title'      => html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) ),
				'type'       => 'taxonomy',
				'type_label' => get_taxonomy( $term->taxonomy )->labels->singular_name,
				'object'     => $term->taxonomy,
				'object_id'  => (int) $term->term_id,
				'url'        => get_term_link( (int) $term->term_id, $term->taxonomy ),
			);
		}
	}

	// Add "Home" link if search term matches. Treat as a page, but switch to custom on add.
	if ( isset( $args['s'] ) ) {
		// Only insert custom "Home" link if there's no Front Page
		$front_page = 'page' === get_option( 'show_on_front' ) ? (int) get_option( 'page_on_front' ) : 0;
		if ( empty( $front_page ) ) {
			$title   = _x( 'Home', 'nav menu home label' );
			$matches = function_exists( 'mb_stripos' ) ? false !== mb_stripos( $title, $args['s'] ) : false !== stripos( $title, $args['s'] );
			if ( $matches ) {
				$items[] = array(
					'id'         => 'home',
					'title'      => $title,
					'type'       => 'custom',
					'type_label' => __( 'Custom Link' ),
					'object'     => '',
					'url'        => home_url(),
				);
			}
		}
	}

	/**
	 * Filters the available menu items during a search request.
	 *
	 * @since 4.5.0
	 *
	 * @param array $items The array of menu items.
	 * @param array $args  Includes 'pagenum' and 's' (search) arguments.
	 */
	$items = apply_filters( 'customize_nav_menu_searched_items', $items, $args );

	return $items;
}

Hooks

apply_filters( ‘customize_nav_menu_searched_items’, array $items, array $args )

Filters the available menu items during a search request.

Changelog

VersionDescription
4.3.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.