WordPress at a glance

get_boundary_post() WP 1.0

Retrieves the boundary post.

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

No Hooks.

Return

null/Array. Array containing the boundary post object if successful, null otherwise.

Usage

get_boundary_post( $in_same_term, $excluded_terms, $start, $taxonomy );
$in_same_term(true/false)
Whether returned post should be in a same taxonomy term.
Default: false
$excluded_terms(array/string)
Array or comma-separated list of excluded term IDs.
Default: ''
$start(true/false)
Whether to retrieve first or last post. Default true
Default: true
$taxonomy(string)
Taxonomy, if $in_same_term is true.
Default: 'category'

Code of get_boundary_post: wp-includes/link-template.php VER 4.9.8

<?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 );
}

Related Functions

From tag: query

More from category: Navigation

No comments
    Hello, !     Log In . Register