get_post()

You are here:

get_post( int|WP_Post|null $post = nullstring $output = OBJECTstring $filter = ‘raw’ )

Retrieves post data given a post ID or post object.

Description Description

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


Top ↑

Parameters Parameters

$post

(int|WP_Post|null) (Optional) Post ID or post object. Defaults to global $post.

Default value: null

$output

(string) (Optional) The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Post object, an associative array, or a numeric array, respectively.

Default value: OBJECT

$filter

(string) (Optional) Type of filter to apply. Accepts ‘raw’, ‘edit’, ‘db’, or ‘display’.

Default value: ‘raw’


Top ↑

Return Return

(WP_Post|array|null) Type corresponding to $output on success or null on failure. When $output is OBJECT, a WP_Post instance is returned.


Top ↑

Source Source

File: wp-includes/post.php

763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) {
    if ( empty( $post ) && isset( $GLOBALS['post'] ) ) {
        $post = $GLOBALS['post'];
    }
 
    if ( $post instanceof WP_Post ) {
        $_post = $post;
    } elseif ( is_object( $post ) ) {
        if ( empty( $post->filter ) ) {
            $_post = sanitize_post( $post, 'raw' );
            $_post = new WP_Post( $_post );
        } elseif ( 'raw' === $post->filter ) {
            $_post = new WP_Post( $post );
        } else {
            $_post = WP_Post::get_instance( $post->ID );
        }
    } else {
        $_post = WP_Post::get_instance( $post );
    }
 
    if ( ! $_post ) {
        return null;
    }
 
    $_post = $_post->filter( $filter );
 
    if ( ARRAY_A == $output ) {
        return $_post->to_array();
    } elseif ( ARRAY_N == $output ) {
        return array_values( $_post->to_array() );
    }
 
    return $_post;
}


Top ↑

Changelog Changelog

Changelog
VersionDescription
1.5.1Introduced.
Was this article helpful?
Dislike 0
Views: 2

Cart

Log in

Create an Account
Back to Top