^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_posts_by_author_sql()用法示例

get_posts_by_author_sql( array|string $post_type,  bool $full = true,  int $post_author = null,  bool $public_only = false )

Retrieve the post SQL based on capability, author, and type.


描述


参数

$post_type

(array|string)
(Required)
Single post type or an array of post types.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

$full

(bool)
(Optional)
Returns a full WHERE statement instead of just an 'andalso' term.

Default value: true

$post_author

(int)
(Optional)
Query posts having a single author ID.

Default value: null

$public_only

(bool)
(Optional)
Only return public posts. Skips cap checks for $current_user.

Default value: false


返回值

(string) SQL WHERE code that can be added to a query.


源代码

File: wp-includes/post.php

function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) {
	global $wpdb;

	if ( is_array( $post_type ) ) {
		$post_types = $post_type;
	} else {
		$post_types = array( $post_type );
	}

	$post_type_clauses = array();
	foreach ( $post_types as $post_type ) {
		$post_type_obj = get_post_type_object( $post_type );
		if ( ! $post_type_obj ) {
			continue;
		}

		/**
		 * Filters the capability to read private posts for a custom post type
		 * when generating SQL for getting posts by author.
		 *
		 * @since 2.2.0
		 * @deprecated 3.2.0 The hook transitioned from "somewhat useless" to "totally useless".
		 *
		 * @param string $cap Capability.
		 */
		if ( ! $cap = apply_filters( 'pub_priv_sql_capability', '' ) ) {
			$cap = current_user_can( $post_type_obj->cap->read_private_posts );
		}

		// Only need to check the cap if $public_only is false.
		$post_status_sql = "post_status = 'publish'";
		if ( false === $public_only ) {
			if ( $cap ) {
				// Does the user have the capability to view private posts? Guess so.
				$post_status_sql .= " OR post_status = 'private'";
			} elseif ( is_user_logged_in() ) {
				// Users can view their own private posts.
				$id = get_current_user_id();
				if ( null === $post_author || ! $full ) {
					$post_status_sql .= " OR post_status = 'private' AND post_author = $id";
				} elseif ( $id == (int) $post_author ) {
					$post_status_sql .= " OR post_status = 'private'";
				} // else none
			} // else none
		}

		$post_type_clauses[] = "( post_type = '" . $post_type . "' AND ( $post_status_sql ) )";
	}

	if ( empty( $post_type_clauses ) ) {
		return $full ? 'WHERE 1 = 0' : '1 = 0';
	}

	$sql = '( '. implode( ' OR ', $post_type_clauses ) . ' )';

	if ( null !== $post_author ) {
		$sql .= $wpdb->prepare( ' AND post_author = %d', $post_author );
	}

	if ( $full ) {
		$sql = 'WHERE ' . $sql;
	}

	return $sql;
}

更新日志

Version 描述
4.3.0 Introduced the ability to pass an array of post types to $post_type.
3.0.0 Introduced.

相关函数

Uses

  • wp-includes/capabilities.php:
    current_user_can()
  • wp-includes/pluggable.php:
    is_user_logged_in()
  • wp-includes/plugin.php:
    apply_filters()
  • wp-includes/user.php:
    get_current_user_id()
  • wp-includes/post.php:
    pub_priv_sql_capability
  • wp-includes/post.php:
    get_post_type_object()
  • wp-includes/wp-db.php:
    wpdb::prepare()
  • Show 2 more uses


Used By

  • wp-includes/class-wp-user-query.php:
    WP_User_Query::parse_orderby()
  • wp-includes/user.php:
    count_user_posts()
  • wp-includes/user.php:
    count_many_users_posts()
  • wp-includes/post.php:
    get_private_posts_cap_sql()

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Paul Bearne

    the full option set to false currently doesn’t return the post filter as you would expect

    see this patch and test for more info

    https://core.trac.wordpress.org/ticket/30354

  2. Example

    
    $where = get_posts_by_author_sql( 'post' );
    echo $where;
    
    // user logged in: WHERE post_type = 'post' AND (post_status = 'publish' OR post_status = 'private')
    // user not logged in: WHERE post_type = 'post' AND (post_status = 'publish')
    
    // get post ID with title "Hello world!" query
    global $wpdb;
    $query = "SELECT ID FROM $wpdb->posts $where AND post_title = %s";
    $post_id = $wpdb->get_var( $wpdb->prepare( $query, 'Hello world!' ) );
    

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈