钩子文档

rest_{$this->taxonomy}_query

💡 云策文档标注

概述

rest_{$this->taxonomy}_query 是一个动态过滤器钩子,用于在通过 REST API 查询分类法术语时修改 get_terms() 的参数。它允许开发者为术语集合请求添加额外参数或设置默认值。

关键要点

  • 这是一个动态钩子,名称中的 $this->taxonomy 指代分类法 slug,例如 rest_category_query 或 rest_post_tag_query。
  • 主要用于过滤 get_terms() 的参数,影响 REST API 中的术语查询结果。
  • 参数包括 $prepared_args(get_terms() 的参数数组)和 $request(WP_REST_Request 对象)。
  • 在 WP_REST_Terms_Controller::get_items() 方法中使用,用于检索与分类法关联的术语。
  • 自 WordPress 4.7.0 版本引入。

📄 原文内容

Filters get_terms() arguments when querying terms via the REST API.

Description

The dynamic portion of the hook name, $this->taxonomy, refers to the taxonomy slug.

Possible hook names include:

  • rest_category_query
  • rest_post_tag_query

Enables adding extra arguments or setting defaults for a terms collection request.

Parameters

$prepared_argsarray
Array of arguments for get_terms() .

More Arguments from get_terms( … $args )

Array or string of arguments. See WP_Term_Query::__construct() for information on accepted arguments.

$requestWP_REST_Request
The REST API request.

Source

$prepared_args = apply_filters( "rest_{$this->taxonomy}_query", $prepared_args, $request );

Changelog

Version Description
4.7.0 Introduced.