WordPress at a glance

get_boundary_post() WP 2.8.0

Retrieves the first or last post of the site (by publish date).

The first or last post can be retrieved from the same term of the current post. You can also specify the terms (elements of the taxonomy), posts from which you want to exclude. See parameters: $in_same_term and $excluded_terms.

No Hooks.

Return

Array/null.

  • Array — when the data was retrieved.
  • Empty Array — when there are no posts (for example, there are no posts on the site).
  • null — when:
    • global variable $post not set.
    • or current page is not is_single().
    • or current page is is_attachment().
    • or specified taxonomy doesn't exists.

Usage

get_boundary_post( $in_same_term, $excluded_terms, $start, $taxonomy );
$in_same_term(true/false)
Whether a returned post should be in the same term as the current post.
true - get the first/last post from the same term (category) as the current post.
Default: false
$excluded_terms(array/string)
ID of terms (separated by commas or as an array), the posts in which will not be taken into account when determining the first or last post.
Default: ''
$start(true/false)
true — if you want to show the first post. false — if you want to show the last post.
Default: true
$taxonomy(string)
The name of the taxonomy for the $excluded_categories parameter.
Default: 'category'

Examples

#1 Get the title of the last post of the current category

"Current category" is the category to which current post belongs (it assumes that the function is called on the post page).

$last = get_boundary_post( true, '', false )[0];
echo $last->post_title;

#2 Get the first post on the blog

$first_post = get_boundary_post()[0];

echo $first_post->post_title;

#3 Other examples

// the latest post on the blog, excluding categories 5 and 20
$last_post = get_boundary_post( false, '5,20', true )[0];

// last post from category of the current post
$last_post_of_the_cat = get_boundary_post( true, '', false )[0];

// the latest post of `my_tax` taxonomy
$last_post = get_boundary_post( false, '', true, 'my_tax' )[0];

#4 What the function returns

The function returns an array of [WP_Post](/function/WP_Post) objects.

$boundary_post = get_boundary_post();

/*
Array
(
	[0] => WP_Post Object (
			[ID] => 1000
			[post_author] => 1
			[post_date] => 2009-05-15 14:48:32
			[post_date_gmt] => 2009-05-15 21:48:32
			[post_content] => Текст поста
			[post_title] => Edge Case: Nested And Mixed Lists
			[post_excerpt] => 
			[post_status] => publish
			[comment_status] => closed
			[ping_status] => closed
			[post_password] => 
			[post_name] => edge-case-nested-and-mixed-lists
			[to_ping] => 
			[pinged] => 
			[post_modified] => 2009-05-15 14:48:32
			[post_modified_gmt] => 2009-05-15 21:48:32
			[post_content_filtered] => 
			[post_parent] => 0
			[guid] => http://wptest.io/demo/?p=1000
			[menu_order] => 0
			[post_type] => post
			[post_mime_type] => 
			[comment_count] => 0
			[filter] => raw
		)
)
*/

Code of get boundary post: wp-includes/link-template.php WP 5.2.2

<?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