get_posts_by_author_sql()
Retrieves WHERE part of the SQL query where posts with 'private' post status for authenticated users will be taken into account.
Used By: count_many_users_posts(), count_user_posts()
Hooks from the function
Return
String
. SQL WHERE code that can be added to a query.
Usage
get_posts_by_author_sql( $post_type, $full, $post_author, $public_only );
- $post_type(array/string) (required)
- Single post type or an array of post types.
- $full(true/false)
Returns a full WHERE statement instead of just an 'andalso' term.
- true
WHERE ( ( post_type = 'post' AND ( post_status = 'publish' OR post_status = 'private' ) ) )
- false
( ( post_type = 'post' AND ( post_status = 'publish' OR post_status = 'private' ) ) )
Default: true
- true
- $post_author(int)
- Add author ID into the query:
post_author = $post_author
instead the ID of the current user.
Default: null - $public_only(true/false)
- Return only
public
posts. Skips cap checks for $current_user (check for private status) — all private posts will be skipped for the current user.
Default: false
Examples
#1 Usage demonstration
$where = get_posts_by_author_sql( 'post' ); echo $where; // Authorized: WHERE ( ( post_type = 'post' AND ( post_status = 'publish' OR post_status = 'private' ) ) ) // Not Authorized: WHERE ( post_type = 'post' AND ( post_status = 'publish' ) ) // Get post ID with "Hello world!" title global $wpdb; $query = "SELECT ID FROM $wpdb->posts $where AND post_title = %s"; $post_id = $wpdb->get_var( $wpdb->prepare( $query, 'Hello world!' ) );
Notes
- See: get_private_posts_cap_sql()
- Global. wpdb. $wpdb WordPress database abstraction object.
Changelog
Since 3.0.0 | Introduced. |
Since 4.3.0 | Introduced the ability to pass an array of post types to $post_type. |