wp_parse_id_list()
云策文档标注
概述
wp_parse_id_list() 函数用于清理和标准化 ID 列表,支持数组、逗号或空格分隔的字符串输入,返回一个唯一的整数数组。
关键要点
- 参数 $input_list 可以是数组或字符串,必需,表示 ID 列表。
- 返回值是 int[] 类型,即经过清理和去重的整数数组。
- 内部实现依赖于 wp_parse_list() 函数进行初步转换,然后使用 absint() 和 array_unique() 确保 ID 为正整数且唯一。
代码示例
function wp_parse_id_list( $input_list ) {
$input_list = wp_parse_list( $input_list );
return array_unique( array_map( 'absint', $input_list ) );
}注意事项
- 该函数自 WordPress 3.0.0 版本引入,在 5.1.0 版本重构以使用 wp_parse_list()。
- 广泛用于多个核心类和方法中,如 WP_Query 相关类、隐私请求处理、小部件内容检查等。
原文内容
Cleans up an array, comma- or space-separated list of IDs.
Parameters
$input_listarray|stringrequired-
List of IDs.
Source
function wp_parse_id_list( $input_list ) {
$input_list = wp_parse_list( $input_list );
return array_unique( array_map( 'absint', $input_list ) );
}
Changelog
| Version | Description |
|---|---|
| 5.1.0 | Refactored to use wp_parse_list() . |
| 3.0.0 | Introduced. |