函数文档

_wp_ajax_menu_quick_search()

💡 云策文档标注

概述

_wp_ajax_menu_quick_search() 是一个 WordPress AJAX 函数,用于处理菜单快速搜索请求,根据请求参数返回 JSON 或 HTML 格式的响应。它支持搜索文章类型和分类法,并可通过过滤器自定义查询参数。

关键要点

  • 函数接受一个可选数组参数 $request,包含搜索类型、对象类型、查询字符串和响应格式等。
  • 响应格式默认为 'json',可选 'markup'(HTML 格式),使用 Walker_Nav_Menu_Checklist 处理 HTML 输出。
  • 根据 $type 参数处理两种主要场景:'get-post-item' 用于获取单个文章或分类项,'quick-search-posttype/taxonomy-*' 用于批量搜索。
  • 支持通过 apply_filters('wp_ajax_menu_quick_search_args', $query_args) 过滤器自定义查询参数。
  • 函数内部使用 WP_Query 进行文章搜索,get_terms() 进行分类法搜索,并调用 walk_nav_menu_tree() 和 wp_json_encode() 生成输出。

代码示例

// 示例调用:搜索文章类型 'post' 的快速搜索
$request = array(
    'type' => 'quick-search-posttype-post',
    'q' => 'example',
    'response-format' => 'json'
);
_wp_ajax_menu_quick_search($request);

注意事项

  • 函数直接输出响应内容,不返回值,适用于 AJAX 处理场景。
  • 确保请求参数正确,如 $type 需匹配特定模式,否则可能无输出。
  • 使用过滤器时,注意参数结构,避免破坏默认查询逻辑。

📄 原文内容

Prints the appropriate response to a menu quick search.

Parameters

$requestarrayoptional
The unsanitized request values.

Default:array()

Source

function _wp_ajax_menu_quick_search( $request = array() ) {
	$args            = array();
	$type            = isset( $request['type'] ) ? $request['type'] : '';
	$object_type     = isset( $request['object_type'] ) ? $request['object_type'] : '';
	$query           = isset( $request['q'] ) ? $request['q'] : '';
	$response_format = isset( $request['response-format'] ) ? $request['response-format'] : '';

	if ( ! $response_format || ! in_array( $response_format, array( 'json', 'markup' ), true ) ) {
		$response_format = 'json';
	}

	if ( 'markup' === $response_format ) {
		$args['walker'] = new Walker_Nav_Menu_Checklist();
	}

	if ( 'get-post-item' === $type ) {
		if ( post_type_exists( $object_type ) ) {
			if ( isset( $request['ID'] ) ) {
				$object_id = (int) $request['ID'];

				if ( 'markup' === $response_format ) {
					echo walk_nav_menu_tree(
						array_map( 'wp_setup_nav_menu_item', array( get_post( $object_id ) ) ),
						0,
						(object) $args
					);
				} elseif ( 'json' === $response_format ) {
					echo wp_json_encode(
						array(
							'ID'         => $object_id,
							'post_title' => get_the_title( $object_id ),
							'post_type'  => get_post_type( $object_id ),
						)
					);
					echo "n";
				}
			}
		} elseif ( taxonomy_exists( $object_type ) ) {
			if ( isset( $request['ID'] ) ) {
				$object_id = (int) $request['ID'];

				if ( 'markup' === $response_format ) {
					echo walk_nav_menu_tree(
						array_map( 'wp_setup_nav_menu_item', array( get_term( $object_id, $object_type ) ) ),
						0,
						(object) $args
					);
				} elseif ( 'json' === $response_format ) {
					$post_obj = get_term( $object_id, $object_type );
					echo wp_json_encode(
						array(
							'ID'         => $object_id,
							'post_title' => $post_obj->name,
							'post_type'  => $object_type,
						)
					);
					echo "n";
				}
			}
		}
	} elseif ( preg_match( '/quick-search-(posttype|taxonomy)-([a-zA-Z0-9_-]*b)/', $type, $matches ) ) {
		if ( 'posttype' === $matches[1] && get_post_type_object( $matches[2] ) ) {
			$post_type_obj = _wp_nav_menu_meta_box_object( get_post_type_object( $matches[2] ) );
			$query_args    = 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,
				'search_columns'         => array( 'post_title' ),
			);
			/**
			 * Filter the menu quick search arguments.
			 *
			 * @since 6.9.0
			 *
			 * @param array $args {
			 *     Menu quick search arguments.
			 *
			 *     @type boolean      $no_found_rows          Whether to return found rows data. Default true.
			 *     @type boolean      $update_post_meta_cache Whether to update post meta cache. Default false.
			 *     @type boolean      $update_post_term_cache Whether to update post term cache. Default false.
			 *     @type int          $posts_per_page         Number of posts to return. Default 10.
			 *     @type string       $post_type              Type of post to return.
			 *     @type string       $s                      Search query.
			 *     @type array        $search_columns         Which post table columns to query.
			 * }
			*/
			$query_args = apply_filters( 'wp_ajax_menu_quick_search_args', $query_args );
			$args       = array_merge( $args, $query_args );

			if ( isset( $post_type_obj->_default_query ) ) {
				$args = array_merge( $args, (array) $post_type_obj->_default_query );
			}

			$search_results_query = new WP_Query( $args );
			if ( ! $search_results_query->have_posts() ) {
				return;
			}

			while ( $search_results_query->have_posts() ) {
				$post = $search_results_query->next_post();

				if ( 'markup' === $response_format ) {
					$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'         => $post->ID,
							'post_title' => get_the_title( $post->ID ),
							'post_type'  => $matches[2],
						)
					);
					echo "n";
				}
			}
		} elseif ( 'taxonomy' === $matches[1] ) {
			$terms = get_terms(
				array(
					'taxonomy'   => $matches[2],
					'name__like' => $query,
					'number'     => 10,
					'hide_empty' => false,
				)
			);

			if ( empty( $terms ) || is_wp_error( $terms ) ) {
				return;
			}

			foreach ( (array) $terms as $term ) {
				if ( 'markup' === $response_format ) {
					echo walk_nav_menu_tree(
						array_map( 'wp_setup_nav_menu_item', array( $term ) ),
						0,
						(object) $args
					);
				} elseif ( 'json' === $response_format ) {
					echo wp_json_encode(
						array(
							'ID'         => $term->term_id,
							'post_title' => $term->name,
							'post_type'  => $matches[2],
						)
					);
					echo "n";
				}
			}
		}
	}
}

Hooks

apply_filters( ‘wp_ajax_menu_quick_search_args’, array $args )

Filter the menu quick search arguments.

Changelog

Version Description
3.0.0 Introduced.