函数文档

wp_get_post_revisions()

💡 云策文档标注

概述

wp_get_post_revisions() 函数用于获取指定文章的所有修订版本。它基于 WP_Query 参数检索修订,并返回修订对象或 ID 数组。

关键要点

  • 函数返回指定文章的修订版本数组,若无修订则返回空数组。
  • 参数 $post 可选,可以是文章 ID 或 WP_Post 对象,默认为全局 $post。
  • 参数 $args 可选,用于传递检索修订的额外参数,如排序方式。
  • 内部使用 get_children() 函数,并检查修订是否启用。
  • 返回类型为 WP_Post[] 或 int[],具体取决于参数设置。

代码示例

function wp_get_post_revisions( $post = 0, $args = null ) {
    $post = get_post( $post );

    if ( ! $post || empty( $post->ID ) ) {
        return array();
    }

    $defaults = array(
        'order'         => 'DESC',
        'orderby'       => 'date ID',
        'check_enabled' => true,
    );
    $args     = wp_parse_args( $args, $defaults );

    if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) ) {
        return array();
    }

    $args = array_merge(
        $args,
        array(
            'post_parent' => $post->ID,
            'post_type'   => 'revision',
            'post_status' => 'inherit',
        )
    );

    $revisions = get_children( $args );

    if ( ! $revisions ) {
        return array();
    }

    return $revisions;
}

注意事项

  • 使用前需确保文章修订功能已启用,可通过 wp_revisions_enabled() 检查。
  • 参数 $args 支持 WP_Query 文档中列出的所有参数,用于自定义检索条件。
  • 函数自 WordPress 2.6.0 版本引入,兼容性良好。

📄 原文内容

Returns all revisions of specified post.

Description

See also

Parameters

$postint|WP_Postoptional
Post ID or WP_Post object. Default is global $post.
$argsarray|nulloptional
Arguments for retrieving post revisions.

Default:null

Return

WP_Post[]|int[] Array of revision objects or IDs, or an empty array if none.

More Information

See the parameters section of the WP_Query documentation for a list of parameters that the parameter $args accepts.

Source

function wp_get_post_revisions( $post = 0, $args = null ) {
	$post = get_post( $post );

	if ( ! $post || empty( $post->ID ) ) {
		return array();
	}

	$defaults = array(
		'order'         => 'DESC',
		'orderby'       => 'date ID',
		'check_enabled' => true,
	);
	$args     = wp_parse_args( $args, $defaults );

	if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) ) {
		return array();
	}

	$args = array_merge(
		$args,
		array(
			'post_parent' => $post->ID,
			'post_type'   => 'revision',
			'post_status' => 'inherit',
		)
	);

	$revisions = get_children( $args );

	if ( ! $revisions ) {
		return array();
	}

	return $revisions;
}

Changelog

Version Description
2.6.0 Introduced.