函数文档

get_post_types_by_support()

💡 云策文档标注

概述

get_post_types_by_support() 函数用于检索支持特定功能的所有文章类型名称列表。它通过全局变量 $_wp_post_type_features 和 wp_filter_object_list() 实现过滤。

关键要点

  • 参数 $feature:必需,可以是单个功能字符串或功能数组,指定要匹配的功能。
  • 参数 $operator:可选,默认为 'and',指定逻辑操作:'or' 表示数组任一元素匹配即可,'and' 表示所有元素必须匹配,'not' 表示无元素匹配。
  • 返回值:返回一个字符串数组,包含符合条件的文章类型名称。
  • 内部实现:使用 array_fill_keys() 和 wp_filter_object_list() 基于 $_wp_post_type_features 进行过滤。

代码示例

function get_post_types_by_support( $feature, $operator = 'and' ) {
	global $_wp_post_type_features;

	$features = array_fill_keys( (array) $feature, true );

	return array_keys( wp_filter_object_list( $_wp_post_type_features, $features, $operator ) );
}

📄 原文内容

Retrieves a list of post type names that support a specific feature.

Parameters

$featurearray|stringrequired
Single feature or an array of features the post types should support.
$operatorstringoptional
The logical operation to perform. 'or' means only one element from the array needs to match; 'and' means all elements must match; 'not' means no elements may match. Default 'and'.

Return

string[] A list of post type names.

Source

function get_post_types_by_support( $feature, $operator = 'and' ) {
	global $_wp_post_type_features;

	$features = array_fill_keys( (array) $feature, true );

	return array_keys( wp_filter_object_list( $_wp_post_type_features, $features, $operator ) );
}

Changelog

Version Description
4.5.0 Introduced.