get_post

The timeline below displays how wordpress function get_post has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.9

/**
 * 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.
 *
 * @since 1.5.1
 *
 * @global WP_Post $post Global post object.
 *
 * @param int|WP_Post|null $post   Optional. Post ID or post object. `null`, `false`, `0` and other PHP falsey values
 *                                 return the current global post inside the loop. A numerically valid post ID that
 *                                 points to a non-existent post returns `null`. Defaults to global $post.
 * @param string           $output 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 OBJECT.
 * @param string           $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
 *                                 or 'display'. Default 'raw'.
 * @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.
 */
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;
}

WordPress Version: 5.8

/**
 * 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.
 *
 * @since 1.5.1
 *
 * @global WP_Post $post Global post object.
 *
 * @param int|WP_Post|null $post   Optional. Post ID or post object. `null`, `false`, `0` and other PHP falsey
 *                                 values return the current global post inside the loop. A numerically valid post
 *                                 ID that points to a non-existent post returns `null`. Defaults to global $post.
 * @param string           $output 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 OBJECT.
 * @param string           $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
 *                                 or 'display'. Default 'raw'.
 * @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.
 */
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;
}

WordPress Version: 5.7

/**
 * 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.
 *
 * @since 1.5.1
 *
 * @global WP_Post $post Global post object.
 *
 * @param int|WP_Post|null $post   Optional. Post ID or post object. `null`, `false`, `0` and other PHP falsey
 *                                 values return the current global post inside the loop. A numerically valid post
 *                                 ID that points to a non-existent post returns `null`. Defaults to global $post.
 * @param string           $output 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 OBJECT.
 * @param string           $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
 *                                 or 'display'. Default 'raw'.
 * @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.
 */
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;
}

WordPress Version: 5.5

/**
 * 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.
 *
 * @since 1.5.1
 *
 * @global WP_Post $post Global post object.
 *
 * @param int|WP_Post|null $post   Optional. Post ID or post object. Defaults to global $post.
 * @param string           $output 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 OBJECT.
 * @param string           $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
 *                                 or 'display'. Default 'raw'.
 * @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.
 */
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;
}

WordPress Version: 5.4

/**
 * 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.
 *
 * @since 1.5.1
 *
 * @global WP_Post $post Global post object.
 *
 * @param int|WP_Post|null $post   Optional. Post ID or post object. Defaults to global $post.
 * @param string           $output 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 OBJECT.
 * @param string           $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
 *                                 or 'display'. Default 'raw'.
 * @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.
 */
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;
}

WordPress Version: 5.3

/**
 * 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.
 *
 * @since 1.5.1
 *
 * @global WP_Post $post Global post object.
 *
 * @param int|WP_Post|null $post   Optional. Post ID or post object. Defaults to global $post.
 * @param string           $output 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 OBJECT.
 * @param string           $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
 *                                 or 'display'. Default 'raw'.
 * @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.
 */
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 ($output == ARRAY_A) {
        return $_post->to_array();
    } elseif ($output == ARRAY_N) {
        return array_values($_post->to_array());
    }
    return $_post;
}

WordPress Version: 4.7

/**
 * 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.
 *
 * @since 1.5.1
 *
 * @global WP_Post $post
 *
 * @param int|WP_Post|null $post   Optional. Post ID or post object. Defaults to global $post.
 * @param string           $output 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 OBJECT.
 * @param string           $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
 *                                 or 'display'. Default 'raw'.
 * @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.
 */
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 ($output == ARRAY_A) {
        return $_post->to_array();
    } elseif ($output == ARRAY_N) {
        return array_values($_post->to_array());
    }
    return $_post;
}

WordPress Version: 4.6

/**
 * 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.
 *
 * @since 1.5.1
 *
 * @global WP_Post $post
 *
 * @param int|WP_Post|null $post   Optional. Post ID or post object. Defaults to global $post.
 * @param string           $output Optional, default is Object. Accepts OBJECT, ARRAY_A, or ARRAY_N.
 *                                 Default OBJECT.
 * @param string           $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
 *                                 or 'display'. Default 'raw'.
 * @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.
 */
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 ($output == ARRAY_A) {
        return $_post->to_array();
    } elseif ($output == ARRAY_N) {
        return array_values($_post->to_array());
    }
    return $_post;
}

WordPress Version: 4.3

/**
 * Retrieves post data given a post ID or post object.
 *
 * See {@link sanitize_post()} for optional $filter values. Also, the parameter
 * $post, must be given as a variable, since it is passed by reference.
 *
 * @since 1.5.1
 *
 * @global WP_Post $post
 *
 * @param int|WP_Post|null $post   Optional. Post ID or post object. Defaults to global $post.
 * @param string           $output Optional, default is Object. Accepts OBJECT, ARRAY_A, or ARRAY_N.
 *                                 Default OBJECT.
 * @param string           $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
 *                                 or 'display'. Default 'raw'.
 * @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.
 */
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 ($output == ARRAY_A) {
        return $_post->to_array();
    } elseif ($output == ARRAY_N) {
        return array_values($_post->to_array());
    }
    return $_post;
}

WordPress Version: 4.2

/**
 * Retrieves post data given a post ID or post object.
 *
 * See {@link sanitize_post()} for optional $filter values. Also, the parameter
 * $post, must be given as a variable, since it is passed by reference.
 *
 * @since 1.5.1
 *
 * @param int|WP_Post $post   Optional. Post ID or post object. Defaults to global $post.
 * @param string      $output Optional, default is Object. Accepts OBJECT, ARRAY_A, or ARRAY_N.
 *                            Default OBJECT.
 * @param string      $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
 *                            or 'display'. Default 'raw'.
 * @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.
 */
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 ($output == ARRAY_A) {
        return $_post->to_array();
    } elseif ($output == ARRAY_N) {
        return array_values($_post->to_array());
    }
    return $_post;
}

WordPress Version: 4.0

/**
 * Retrieves post data given a post ID or post object.
 *
 * See {@link sanitize_post()} for optional $filter values. Also, the parameter
 * $post, must be given as a variable, since it is passed by reference.
 *
 * @since 1.5.1
 *
 * @param int|WP_Post $post   Optional. Post ID or post object. Defaults to global $post.
 * @param string      $output Optional, default is Object. Accepts OBJECT, ARRAY_A, or ARRAY_N.
 *                            Default OBJECT.
 * @param string      $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
 *                            or 'display'. Default 'raw'.
 * @return WP_Post|null WP_Post on success or null on failure.
 */
function get_post($post = null, $output = OBJECT, $filter = 'raw')
{
    if (empty($post) && isset($GLOBALS['post'])) {
        $post = $GLOBALS['post'];
    }
    if (is_a($post, '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 ($output == ARRAY_A) {
        return $_post->to_array();
    } elseif ($output == ARRAY_N) {
        return array_values($_post->to_array());
    }
    return $_post;
}

WordPress Version: 3.9

/**
 * Retrieves post data given a post ID or post object.
 *
 * See {@link sanitize_post()} for optional $filter values. Also, the parameter
 * $post, must be given as a variable, since it is passed by reference.
 *
 * @since 1.5.1
 * @link http://codex.wordpress.org/Function_Reference/get_post
 *
 * @param int|WP_Post $post Optional. Post ID or post object.
 * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
 * @param string $filter Optional, default is raw.
 * @return WP_Post|null WP_Post on success or null on failure
 */
function get_post($post = null, $output = OBJECT, $filter = 'raw')
{
    if (empty($post) && isset($GLOBALS['post'])) {
        $post = $GLOBALS['post'];
    }
    if (is_a($post, '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 ($output == ARRAY_A) {
        return $_post->to_array();
    } elseif ($output == ARRAY_N) {
        return array_values($_post->to_array());
    }
    return $_post;
}

WordPress Version: 3.7

/**
 * Retrieves post data given a post ID or post object.
 *
 * See {@link sanitize_post()} for optional $filter values. Also, the parameter
 * $post, must be given as a variable, since it is passed by reference.
 *
 * @since 1.5.1
 * @link http://codex.wordpress.org/Function_Reference/get_post
 *
 * @param int|object $post Post ID or post object. Optional, default is the current post from the loop.
 * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
 * @param string $filter Optional, default is raw.
 * @return WP_Post|null WP_Post on success or null on failure
 */
function get_post($post = null, $output = OBJECT, $filter = 'raw')
{
    if (empty($post) && isset($GLOBALS['post'])) {
        $post = $GLOBALS['post'];
    }
    if (is_a($post, '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 ($output == ARRAY_A) {
        return $_post->to_array();
    } elseif ($output == ARRAY_N) {
        return array_values($_post->to_array());
    }
    return $_post;
}