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

This function provides a standardized way to appropriately select on the post_status of a post type. The function will return a piece of SQL code that can be added to a WHERE clause; this SQL is constructed to allow all published posts, and all private posts to which the user has access.

<?php get_posts_by_author_sql$post_type$full$post_author$public_only); ?>

get_posts_by_author_sql() 描述



(string) (必填) Post type.

默认值: None


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

默认值: true


(integer) (可选) Query posts having a single author ID.

默认值: null


(bool) (可选) Only return public posts. Skips capability checks for the current user.

默认值: false

get_posts_by_author_sql() 参数



SQL WHERE code that can be added to a query.

get_posts_by_author_sql() 返回值


/* ----------------------------------
 * wordpress之魂 ©
 * ---------------------------------- */
$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!' ) );

get_posts_by_author_sql() 示例


添加于 版本: 3.0

get_posts_by_author_sql() 历史