get_posts_by_author_sql

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

Description

(string) get_posts_by_author_sql( (array|string) $post_type, (constant) $full = true, (constant) $post_author = null, (bool) $public_only = false ); 

Returns (string)

SQL WHERE code that can be added to a query.

Parameters (4)

0. $post_type (array|string)
Single post type or an array of post types.
1. $full — Optional. (constant) => true
Returns a full WHERE statement instead of just an andalso term. Default true.
2. $post_author — Optional. (constant) => null
Query posts having a single author ID. Default null.
3. $public_only — Optional. (bool) => false
Only return public posts. Skips cap checks for $current_user. Default false.

Usage

  1. if ( !function_exists( 'get_posts_by_author_sql' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // Single post type or an array of post types. 
  5. $post_type = null; 
  6.  
  7. // Optional. Returns a full WHERE statement instead of just 
  8. // an 'andalso' term. Default true. 
  9. $full = true; 
  10.  
  11. // Optional. Query posts having a single author ID. Default null. 
  12. $post_author = null; 
  13.  
  14. // Optional. Only return public posts. Skips cap checks for 
  15. // $current_user. Default false. 
  16. $public_only = false; 
  17.  
  18. // NOTICE! Understand what this does before running. 
  19. $result = get_posts_by_author_sql($post_type, $full, $post_author, $public_only); 
  20.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post.php  
  1. function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) { 
  2. global $wpdb; 
  3.  
  4. if ( is_array( $post_type ) ) { 
  5. $post_types = $post_type; 
  6. } else { 
  7. $post_types = array( $post_type ); 
  8.  
  9. $post_type_clauses = array(); 
  10. foreach ( $post_types as $post_type ) { 
  11. $post_type_obj = get_post_type_object( $post_type ); 
  12. if ( ! $post_type_obj ) { 
  13. continue; 
  14.  
  15. /** 
  16. * Filters the capability to read private posts for a custom post type 
  17. * when generating SQL for getting posts by author. 
  18. * @since 2.2.0 
  19. * @deprecated 3.2.0 The hook transitioned from "somewhat useless" to "totally useless". 
  20. * @param string $cap Capability. 
  21. */ 
  22. if ( ! $cap = apply_filters( 'pub_priv_sql_capability', '' ) ) { 
  23. $cap = current_user_can( $post_type_obj->cap->read_private_posts ); 
  24.  
  25. // Only need to check the cap if $public_only is false. 
  26. $post_status_sql = "post_status = 'publish'"; 
  27. if ( false === $public_only ) { 
  28. if ( $cap ) { 
  29. // Does the user have the capability to view private posts? Guess so. 
  30. $post_status_sql .= " OR post_status = 'private'"; 
  31. } elseif ( is_user_logged_in() ) { 
  32. // Users can view their own private posts. 
  33. if ( null === $post_author || ! $full ) { 
  34. $post_status_sql .= " OR post_status = 'private' AND post_author = $id"; 
  35. } elseif ( $id == (int) $post_author ) { 
  36. $post_status_sql .= " OR post_status = 'private'"; 
  37. } // else none 
  38. } // else none 
  39.  
  40. $post_type_clauses[] = "( post_type = '" . $post_type . "' AND ( $post_status_sql ) )"; 
  41.  
  42. if ( empty( $post_type_clauses ) ) { 
  43. return $full ? 'WHERE 1 = 0' : '1 = 0'; 
  44.  
  45. $sql = '( '. implode( ' OR ', $post_type_clauses ) . ' )'; 
  46.  
  47. if ( null !== $post_author ) { 
  48. $sql .= $wpdb->prepare( ' AND post_author = %d', $post_author ); 
  49.  
  50. if ( $full ) { 
  51. $sql = 'WHERE ' . $sql; 
  52.  
  53. return $sql;