Advertisements

get_boundary_post

Definition:
function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) {}

Retrieve boundary post.
Boundary being either the first or last post by publish date within the constraints specified by $in_same_cat or $excluded_categories.

Parameters

  • bool $in_same_cat: Optional. Whether returned post should be in a same category.
  • array|string $excluded_categories: Optional. Array or comma-separated list of excluded category IDs.
  • bool $start: Optional. Whether to retrieve first or last post.

Source code

function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) {

	global $post;



	if ( empty($post) || ! is_single() || is_attachment() )

		return null;



	$cat_array = array();

	if( ! is_array( $excluded_categories ) )

		$excluded_categories = explode( ',', $excluded_categories );



	if ( $in_same_cat || ! empty( $excluded_categories ) ) {

		if ( $in_same_cat )

			$cat_array = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );



		if ( ! empty( $excluded_categories ) ) {

			$excluded_categories = array_map( 'intval', $excluded_categories );

			$excluded_categories = array_diff( $excluded_categories, $cat_array );



			$inverse_cats = array();

			foreach ( $excluded_categories as $excluded_category )

				$inverse_cats[] = $excluded_category * -1;

			$excluded_categories = $inverse_cats;

		}

	}



	$categories = implode( ',', array_merge( $cat_array, $excluded_categories ) );



	$order = $start ? 'ASC' : 'DESC';



	return get_posts( array('numberposts' => 1, 'category' => $categories, 'order' => $order, 'update_post_term_cache' => false, 'update_post_meta_cache' => false) );

}

1230

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: