wp_get_single_post

Definition:
function wp_get_single_post($postid = 0, $mode = OBJECT) {}

Retrieve a single post, based on post ID.
Has categories in ‘post_category’ property or key. Has tags in ‘tags_input’ property or key.

Parameters

  • int $postid: Post ID.
  • string $mode: How to return result, either OBJECT, ARRAY_N, or ARRAY_A.

Return values

returns:Post object or array holding post contents and information

Source code

function wp_get_single_post($postid = 0, $mode = OBJECT) {

	$postid = (int) $postid;



	$post = get_post($postid, $mode);



	if (

		( OBJECT == $mode && empty( $post->ID ) ) ||

		( OBJECT != $mode && empty( $post['ID'] ) )

	)

		return ( OBJECT == $mode ? null : array() );



	// Set categories and tags

	if ( $mode == OBJECT ) {

		$post->post_category = array();

		if ( is_object_in_taxonomy($post->post_type, 'category') )

			$post->post_category = wp_get_post_categories($postid);

		$post->tags_input = array();

		if ( is_object_in_taxonomy($post->post_type, 'post_tag') )

			$post->tags_input = wp_get_post_tags($postid, array('fields' => 'names'));

	} else {

		$post['post_category'] = array();

		if ( is_object_in_taxonomy($post['post_type'], 'category') )

			$post['post_category'] = wp_get_post_categories($postid);

		$post['tags_input'] = array();

		if ( is_object_in_taxonomy($post['post_type'], 'post_tag') )

			$post['tags_input'] = wp_get_post_tags($postid, array('fields' => 'names'));

	}



	return $post;

}

3751

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: