wppaste
WordPress

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

Since
3.0.0, 4.3.0
Source
wp-includes/post.php:7455
Retrieves the post SQL based on capability, author, and type.

Compatibility

WordPress
since 4.3.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$post_typestring|string[]
Single post type or an array of post types.
$fullbooloptional
Returns a full WHERE statement instead of just an 'andalso' term. Default true.Default: true
$post_authorintoptional
Query posts having a single author ID. Default null.Default: null
$public_onlybooloptional
Only return public posts. Skips cap checks for $current_user. Default false.Default: false

Return value

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

Performance profile

How much work a call to get_posts_by_author_sql() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
15–29

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 82.

Plugin surface
1 hook

Third-party callbacks on 'pub_priv_sql_capability' run inside this call, and their cost is not bounded by anything here.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters_deprecated()called directly

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_posts_by_author_sql() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always15–24none
!empty($post_type_clauses) && $post_author !== null25–29->prepare()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8215–2915
8.58215–2915
8.48215–29153 fewer instructions than PHP 8.3
8.38515–3215
8.28515–3215
8.18515–3215
7.48515–3215

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while get_posts_by_author_sql() runs, in this order:

  1. do_action( pub_priv_sql_capability )filter_deprecatedline 7481 (+26 into the body)

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

Uses · 5

Used by · 4

Source code

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;}

Changelog

Introduced in 4.3.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

4.3.0
Introduced the ability to pass an array of post types to $post_type.from the docblock
3.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.