Title: get_posts_by_author_sql
Published: April 25, 2014
Last modified: August 20, 2026

---

# get_posts_by_author_sql( string|string[] $post_type, bool $full = true, int $post_author = null, bool $public_only = false ): string

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#description)
    - [See also](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#see-also)
 * [Parameters](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#return)
 * [More Information](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#wp--skip-link--target)

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

## 󠀁[Description](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#description)󠁿

### 󠀁[See also](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#see-also)󠁿

 * [get_private_posts_cap_sql()](https://developer.wordpress.org/reference/functions/get_private_posts_cap_sql/)

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#parameters)󠁿

 `$post_type`string|string[]required

Single post type or an array of post types.

`$full`booloptional

Returns a full WHERE statement instead of just an `'andalso'` term.

Default:`true`

`$post_author`intoptional

Query posts having a single author ID.

Default:`null`

`$public_only`booloptional

Only return public posts. Skips cap checks for $current_user.

Default:`false`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#return)󠁿

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

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#more-information)󠁿

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.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#source)󠁿

    ```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.
    		 */
    		$cap = apply_filters_deprecated( 'pub_priv_sql_capability', array( '' ), '3.2.0' );

    		if ( ! $cap ) {
    			$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;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/post.php#L7676)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/post.php#L7676-L7744)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#hooks)󠁿

 [apply_filters_deprecated( ‘pub_priv_sql_capability’, string $cap )](https://developer.wordpress.org/reference/hooks/pub_priv_sql_capability/)

Filters the capability to read private posts for a custom post type when generating
SQL for getting posts by author.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#related)󠁿

| Uses | Description | 
| [apply_filters_deprecated()](https://developer.wordpress.org/reference/functions/apply_filters_deprecated/)`wp-includes/plugin.php` |

Fires functions attached to a deprecated filter hook.

  | 
| [current_user_can()](https://developer.wordpress.org/reference/functions/current_user_can/)`wp-includes/capabilities.php` |

Returns whether the current user has the specified capability.

  | 
| [is_user_logged_in()](https://developer.wordpress.org/reference/functions/is_user_logged_in/)`wp-includes/pluggable.php` |

Determines whether the current visitor is a logged in user.

  | 
| [get_current_user_id()](https://developer.wordpress.org/reference/functions/get_current_user_id/)`wp-includes/user.php` |

Gets the current user’s ID.

  | 
| [get_post_type_object()](https://developer.wordpress.org/reference/functions/get_post_type_object/)`wp-includes/post.php` |

Retrieves a post type object by name.

  | 
| [wpdb::prepare()](https://developer.wordpress.org/reference/classes/wpdb/prepare/)`wp-includes/class-wpdb.php` |

Prepares a SQL query for safe execution.

  |

[Show 4 more](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#)

| Used by | Description | 
| [WP_User_Query::parse_orderby()](https://developer.wordpress.org/reference/classes/wp_user_query/parse_orderby/)`wp-includes/class-wp-user-query.php` |

Parses and sanitizes ‘orderby’ keys passed to the user query.

  | 
| [count_user_posts()](https://developer.wordpress.org/reference/functions/count_user_posts/)`wp-includes/user.php` |

Gets the number of posts a user has written.

  | 
| [count_many_users_posts()](https://developer.wordpress.org/reference/functions/count_many_users_posts/)`wp-includes/user.php` |

Gets the number of posts written by a list of users.

  | 
| [get_private_posts_cap_sql()](https://developer.wordpress.org/reference/functions/get_private_posts_cap_sql/)`wp-includes/post.php` |

Retrieves the private post SQL based on capability.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.3.0](https://developer.wordpress.org/reference/since/4.3.0/) | Introduced the ability to pass an array of post types to `$post_type`. | 
| [3.0.0](https://developer.wordpress.org/reference/since/3.0.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#comment-content-164)
 2.    [Paul Bearne](https://profiles.wordpress.org/pbearne/)  [  12 years ago  ](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/#comment-164)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_posts_by_author_sql%2F%23comment-164)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_posts_by_author_sql%2F%23comment-164)
 4.  the full option set to false currently doesn’t return the post filter as you would
     expect
 5.  see this patch and test for more info
 6.  [https://core.trac.wordpress.org/ticket/30354](https://core.trac.wordpress.org/ticket/30354)
 7.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_posts_by_author_sql%2F%3Freplytocom%3D164%23feedback-editor-164)
 8.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/?output_format=md#comment-content-1272)
 9.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/#comment-1272)
 10. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_posts_by_author_sql%2F%23comment-1272)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_posts_by_author_sql%2F%23comment-1272)
 11. **Example**
 12.     ```php
         $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!' ) );
         ```
     
 13.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_posts_by_author_sql%2F%3Freplytocom%3D1272%23feedback-editor-1272)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_posts_by_author_sql%2F)
before being able to contribute a note or feedback.