函数文档

get_others_unpublished_posts()

💡 云策文档标注

概述

get_others_unpublished_posts() 是一个已弃用的 WordPress 函数,用于检索其他用户可编辑的未发布文章(草稿或待审)。自 WordPress 3.1.0 起,建议使用 get_posts() 替代。

关键要点

  • 函数已弃用:自 WordPress 3.1.0 起标记为弃用,推荐使用 get_posts() 进行替代。
  • 功能:检索指定用户 ID 以外的其他用户可编辑的未发布文章,支持草稿、待审或全部类型。
  • 参数:$user_id(必填,排除的用户 ID)和 $type(可选,文章类型,默认为 'any')。
  • 返回值:返回一个文章数组,包含其他用户的未发布文章列表。
  • 内部实现:基于 SQL 查询,使用 get_editable_user_ids() 获取可编辑用户 ID,并通过 wpdb 类执行数据库操作。

代码示例

function get_others_unpublished_posts( $user_id, $type = 'any' ) {
    _deprecated_function( __FUNCTION__, '3.1.0' );

    global $wpdb;

    $editable = get_editable_user_ids( $user_id );

    if ( in_array($type, array('draft', 'pending')) )
        $type_sql = " post_status = '$type' ";
    else
        $type_sql = " ( post_status = 'draft' OR post_status = 'pending' ) ";

    $dir = ( 'pending' == $type ) ? 'ASC' : 'DESC';

    if ( !$editable ) {
        $other_unpubs = '';
    } else {
        $editable = join(',', $editable);
        $other_unpubs = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_title, post_author FROM $wpdb->posts WHERE post_type = 'post' AND $type_sql AND post_author IN ($editable) AND post_author != %d ORDER BY post_modified $dir", $user_id) );
    }

    return apply_filters('get_others_drafts', $other_unpubs);
}

注意事项

  • 此函数已弃用,新开发中应避免使用,改用 get_posts() 或其他现代方法。
  • 函数内部使用 _deprecated_function() 标记弃用,调用时会触发弃用通知。
  • 依赖于 get_editable_user_ids() 和 wpdb 类,确保相关依赖在环境中可用。
  • 返回结果通过 apply_filters('get_others_drafts', $other_unpubs) 应用过滤器,允许自定义输出。

📄 原文内容

Retrieves editable posts from other users.

Description

See also

Parameters

$user_idintrequired
User ID to not retrieve posts from.
$typestringoptional
Post type to retrieve. Accepts 'draft', 'pending' or 'any' (all).
Default 'any'.

Return

array List of posts from others.

Source

function get_others_unpublished_posts( $user_id, $type = 'any' ) {
	_deprecated_function( __FUNCTION__, '3.1.0' );

	global $wpdb;

	$editable = get_editable_user_ids( $user_id );

	if ( in_array($type, array('draft', 'pending')) )
		$type_sql = " post_status = '$type' ";
	else
		$type_sql = " ( post_status = 'draft' OR post_status = 'pending' ) ";

	$dir = ( 'pending' == $type ) ? 'ASC' : 'DESC';

	if ( !$editable ) {
		$other_unpubs = '';
	} else {
		$editable = join(',', $editable);
		$other_unpubs = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_title, post_author FROM $wpdb->posts WHERE post_type = 'post' AND $type_sql AND post_author IN ($editable) AND post_author != %d ORDER BY post_modified $dir", $user_id) );
	}

	return apply_filters('get_others_drafts', $other_unpubs);
}

Changelog

Version Description
3.1.0 Deprecated. Use get_posts()
2.3.0 Introduced.