get_boundary_post()

You are here:

get_boundary_post( bool $in_same_term = falsearray|string $excluded_terms = bool $start = truestring $taxonomy = ‘category’ )

Description Description

Boundary being either the first or last post by publish date within the constraints specified by $in_same_term or $excluded_terms.


Top ↑

Parameters Parameters

$in_same_term

(bool) (Optional) Whether returned post should be in a same taxonomy term.

Default value: false

$excluded_terms

(array|string) (Optional) Array or comma-separated list of excluded term IDs.

Default value: ”

$start

(bool) (Optional) Whether to retrieve first or last post. Default true

Default value: true

$taxonomy

(string) (Optional) Taxonomy, if $in_same_term is true.

Default value: ‘category’


Top ↑

Return Return

(null|array) Array containing the boundary post object if successful, null otherwise.


Top ↑

More Information More Information

get_boundary_post() will set the post pointer to the first post.


Top ↑

Source Source

File: wp-includes/link-template.php

2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
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 );
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.8.0 Introduced.

Top ↑

Was this article helpful?
Dislike 0
Views: 7