# get_posts_by_author_sql()

URL: https://chugunov.pro/api-wordpress/functions/get_posts_by_author_sql/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 4.3.0.

## Сигнатура

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

## Описание

См. alsoget_private_posts_cap_sql()

## Параметры

- `$post_type` `string|string[]` — обязательный. Один тип записи или массив типов записей.
- `$full` `bool` — необязательный, по умолчанию `true`. Возвращает полное выражение WHERE вместо только части 'andalso'.
- `$post_author` `int` — необязательный, по умолчанию `null`. Запрашивает записи с одним идентификатором автора.
- `$public_only` `bool` — необязательный, по умолчанию `false`. Возвращать только публичные записи. Пропускает проверки прав доступа для $current_user.

## Возвращаемое значение

`string`

## Исходный код

Файл: `wp-includes/post.php:7454`

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

## История изменений

- 4.3.0 — Introduced the ability to pass an array of post types to $post_type.
- 3.0.0 — Introduced.

## Связанные

Использует: [`apply_filters_deprecated`](https://chugunov.pro/api-wordpress/functions/apply_filters_deprecated/), [`current_user_can`](https://chugunov.pro/api-wordpress/functions/current_user_can/), [`is_user_logged_in`](https://chugunov.pro/api-wordpress/functions/is_user_logged_in/), [`get_current_user_id`](https://chugunov.pro/api-wordpress/functions/get_current_user_id/), [`get_post_type_object`](https://chugunov.pro/api-wordpress/functions/get_post_type_object/), `wpdb::prepare`.
Используется в: `WP_User_Query::parse_orderby`, [`count_user_posts`](https://chugunov.pro/api-wordpress/functions/count_user_posts/), [`count_many_users_posts`](https://chugunov.pro/api-wordpress/functions/count_many_users_posts/), [`get_private_posts_cap_sql`](https://chugunov.pro/api-wordpress/functions/get_private_posts_cap_sql/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/get_posts_by_author_sql/
