Advertisements

get_posts_by_author_sql

Definition:
function get_posts_by_author_sql( $post_type, $full = true, $post_author = null ) {}

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

Parameters

  • string $post_type: Post type.
  • bool $full: Optional. Returns a full WHERE statement instead of just an ‘andalso’ term.
  • int $post_author: Optional. Query posts having a single author ID.

Return values

returns:SQL WHERE code that can be added to a query.

Defined filters

  • pub_priv_sql_capability
    apply_filters( 'pub_priv_sql_capability', '' )

Source code

function get_posts_by_author_sql( $post_type, $full = true, $post_author = null ) {

	global $user_ID, $wpdb;



	// Private posts

	$post_type_obj = get_post_type_object( $post_type );

	if ( ! $post_type_obj )

		return $full ? 'WHERE 1 = 0' : ' 1 = 0 ';



	// This hook is deprecated. Why you'd want to use it, I dunno.

	if ( ! $cap = apply_filters( 'pub_priv_sql_capability', '' ) )

		$cap = $post_type_obj->cap->read_private_posts;



	if ( $full ) {

		if ( null === $post_author ) {

			$sql = $wpdb->prepare( 'WHERE post_type = %s AND ', $post_type );

		} else {

			$sql = $wpdb->prepare( 'WHERE post_author = %d AND post_type = %s AND ', $post_author, $post_type );

		}

	} else {

		$sql = '';

	}



	$sql .= "(post_status = 'publish'";



	if ( current_user_can( $cap ) ) {

		// Does the user have the capability to view private posts? Guess so.

		$sql .= " OR post_status = 'private'";

	} elseif ( is_user_logged_in() ) {

		// Users can view their own private posts.

		$id = (int) $user_ID;

		if ( null === $post_author || ! $full ) {

			$sql .= " OR post_status = 'private' AND post_author = $id";

		} elseif ( $id == (int) $post_author ) {

			$sql .= " OR post_status = 'private'";

		} // else none

	} // else none



	$sql .= ')';



	return $sql;

}

1574

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: