函数文档

get_users_drafts()

💡 云策文档标注

概述

get_users_drafts() 函数用于从数据库中检索指定用户的草稿文章,返回包含文章ID和标题的数组。该函数通过SQL查询实现,并提供了一个过滤器钩子以允许自定义查询字符串。

关键要点

  • 函数参数:$user_id(整数,必需),指定用户ID。
  • 返回值:数组,包含草稿文章的ID和post_title。
  • 核心实现:使用$wpdb->prepare()准备SQL查询,查询条件包括post_type='post'、post_status='draft'和post_author匹配用户ID,按post_modified降序排序。
  • 过滤器钩子:apply_filters('get_users_drafts', $query),允许修改SQL查询字符串。
  • 相关函数:涉及wpdb::get_results()、wpdb::prepare()和apply_filters()等。
  • 版本历史:自WordPress 2.0.0引入。

代码示例

function get_users_drafts( $user_id ) {
    global $wpdb;
    $query = $wpdb->prepare( "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author = %d ORDER BY post_modified DESC", $user_id );
    $query = apply_filters( 'get_users_drafts', $query );
    return $wpdb->get_results( $query );
}

📄 原文内容

Retrieve the user’s drafts.

Parameters

$user_idintrequired
User ID.

Return

array

Source

function get_users_drafts( $user_id ) {
	global $wpdb;
	$query = $wpdb->prepare( "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author = %d ORDER BY post_modified DESC", $user_id );

	/**
	 * Filters the SQL query string for the user's drafts query.
	 *
	 * @since 2.0.0
	 *
	 * @param string $query The user's drafts query string.
	 */
	$query = apply_filters( 'get_users_drafts', $query );
	return $wpdb->get_results( $query );
}

Hooks

apply_filters( ‘get_users_drafts’, string $query )

Filters the SQL query string for the user’s drafts query.

Changelog

Version Description
2.0.0 Introduced.