检索边界帖。
原型
get_boundary_post( bool $in_same_term = false, array|string $excluded_terms = '', bool $start = true, string $taxonomy = 'category' )
描述
边界是由$ in_same_term或$ excluded_terms指定的约束内的发布日期的第一个或最后一个帖子。
参数
$in_same_term
(bool)
(Optional)
返回的帖子是否应该在同一个分类学术语中。
$excluded_terms
(array|string)
(Optional)
排除的术语ID的数组或逗号分隔列表。
$start
(bool)
(Optional)
是否检索第一篇或最后一篇文章。默认为true
$taxonomy
(string)
(Optional)
分类法,如果$ in_same_term为真。
返回值
(null|array)
如果成功则包含边界post对象的数组,否则返回null。
源文件
路径:wp-includes/link-template.php
<?php
...
function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
$post = get_post();
if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
return null;
$query_args = array(
'posts_per_page' => 1,
'order' => $start ? 'ASC' : 'DESC',
'update_post_term_cache' => false,
'update_post_meta_cache' => false
);
$term_array = array();
if ( ! is_array( $excluded_terms ) ) {
if ( ! empty( $excluded_terms ) )
$excluded_terms = explode( ',', $excluded_terms );
else
$excluded_terms = array();
}
if ( $in_same_term || ! empty( $excluded_terms ) ) {
if ( $in_same_term )
$term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
if ( ! empty( $excluded_terms ) ) {
$excluded_terms = array_map( 'intval', $excluded_terms );
$excluded_terms = array_diff( $excluded_terms, $term_array );
$inverse_terms = array();
foreach ( $excluded_terms as $excluded_term )
$inverse_terms[] = $excluded_term * -1;
$excluded_terms = $inverse_terms;
}
$query_args[ 'tax_query' ] = array( array(
'taxonomy' => $taxonomy,
'terms' => array_merge( $term_array, $excluded_terms )
) );
}
return get_posts( $query_args );
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/get_boundary_post/