get_post

Definition:
function &get_post(&$post, $output = OBJECT, $filter = 'raw') {}

Retrieves post data given a post ID or post object.
See sanitize_post() for optional $filter values. Also, the parameter $post, must be given as a variable, since it is passed by reference.

Parameters

  • int|object $post: Post ID or post object.
  • string $output: Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
  • string $filter: Optional, default is raw.
  • &$post

Return values

returns:Post data

Source code

function &get_post(&$post, $output = OBJECT, $filter = 'raw') {

	global $wpdb;

	$null = null;



	if ( empty($post) ) {

		if ( isset($GLOBALS['post']) )

			$_post = & $GLOBALS['post'];

		else

			return $null;

	} elseif ( is_object($post) && empty($post->filter) ) {

		_get_post_ancestors($post);

		$_post = sanitize_post($post, 'raw');

		wp_cache_add($post->ID, $_post, 'posts');

	} elseif ( is_object($post) && 'raw' == $post->filter ) {

		$_post = $post;

	} else {

		if ( is_object($post) )

			$post_id = $post->ID;

		else

			$post_id = $post;



		$post_id = (int) $post_id;

		if ( ! $_post = wp_cache_get($post_id, 'posts') ) {

			$_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id));

			if ( ! $_post )

				return $null;

			_get_post_ancestors($_post);

			$_post = sanitize_post($_post, 'raw');

			wp_cache_add($_post->ID, $_post, 'posts');

		}

	}



	if ($filter != 'raw')

		$_post = sanitize_post($_post, $filter);



	if ( $output == OBJECT ) {

		return $_post;

	} elseif ( $output == ARRAY_A ) {

		$__post = get_object_vars($_post);

		return $__post;

	} elseif ( $output == ARRAY_N ) {

		$__post = array_values(get_object_vars($_post));

		return $__post;

	} else {

		return $_post;

	}

}

1568

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: