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:7676
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
- Scaling
- Scales with input
- Instructions
- 15–29
- Plugin surface
- 1 hook
- Called by
- 4
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 82.
Third-party callbacks on 'pub_priv_sql_capability' run inside this call, and their cost is not bounded by anything here.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 15–24 | none |
!empty($post_type_clauses) && $post_author !== null | 25–29 | ->prepare() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 82 | 15–29 | 15 | |
| 8.5 | 82 | 15–29 | 15 | |
| 8.4 | 82 | 15–29 | 15 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 85 | 15–32 | 15 | |
| 8.2 | 85 | 15–32 | 15 | |
| 8.1 | 85 | 15–32 | 15 | |
| 7.4 | 85 | 15–32 | 15 |
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:
- do_action( pub_priv_sql_capability )filter_deprecatedline 7702 (+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
- get_post_type_object()Retrieves a post type object by name.
- apply_filters_deprecated()Fires functions attached to a deprecated filter hook.
- current_user_can()Returns whether the current user has the specified capability.
- is_user_logged_in()Determines whether the current visitor is a logged in user.
- get_current_user_id()Gets the current user's ID.
Used by · 4
- WP_User_Query::parse_orderby()Parses and sanitizes 'orderby' keys passed to the user query.
- count_many_users_posts()Gets the number of posts written by a list of users.
- count_user_posts()Gets the number of posts a user has written.
- get_private_posts_cap_sql()Retrieves the private post SQL based on capability.
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.
Signature, return type and hooks compared across 5 parsed releases.
$post_type.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.