Retrieve the post SQL based on capability, author, and type.
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.
<?php get_posts_by_author_sql( $post_type, $full, $post_author, $public_only); ?>
<?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!' ) ); ?>
get_posts_by_author_sql() is located in