_acf_query_restrict_unreadable_posts()
The internal ACF function to hide posts the current user can't read from acf_get_grouped_posts()
WP_Query only applies perm=readable to private posts, and checks an attachment's own inherit status rather than its parent's. This also hides other users' draft, pending and scheduled posts the current user can't edit, and attachments that inherit a status from a parent the user can't read.
When the query has the field's post types in acf_field_post_types, it also hides other users' posts of post types that aren't public, unless the field lists them or the user can open their list of posts in the admin or edit other users' posts, matching acf_user_can_read_post().
Внутренняя функция — эта функция рассчитана на использование самим ядром. Не рекомендуется использовать эту функцию в своем коде.
Хуков нет.
Возвращает
string. The potentially modified WHERE clause.
Использование
_acf_query_restrict_unreadable_posts( $where, $wp_query );
- $where(строка) (обязательный)
- The WHERE clause of the query.
- $wp_query(WP_Query) (обязательный)
- The WP_Query.
Список изменений
| С версии 6.8.11 | Введена. |
Код _acf_query_restrict_unreadable_posts() acf query restrict unreadable posts ACF 6.8.11
function _acf_query_restrict_unreadable_posts( $where, $wp_query ) {
global $wpdb;
if ( ! $wp_query->get( 'acf_restrict_unreadable_posts' ) ) {
return $where;
}
$post_types = acf_get_array( $wp_query->get( 'post_type' ) );
if ( empty( $post_types ) || in_array( 'any', $post_types, true ) ) {
$post_types = get_post_types();
}
$field_post_types = $wp_query->get( 'acf_field_post_types', null );
$readable_post_types = array();
$conditions = array( $wpdb->prepare( "{$wpdb->posts}.post_author = %d", get_current_user_id() ) );
foreach ( $post_types as $post_type ) {
// Other users' posts of these post types are left out, so only the user's own posts are found.
if ( is_array( $field_post_types ) && _acf_post_type_is_hidden_from_search( $post_type, $field_post_types ) ) {
continue;
}
$statuses = _acf_get_unreadable_post_statuses( $post_type );
if ( empty( $statuses ) ) {
$readable_post_types[] = $post_type;
continue;
}
$statuses = implode( "','", array_map( 'esc_sql', $statuses ) );
$condition = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $post_type ) . " AND {$wpdb->posts}.post_status NOT IN ('$statuses')";
// Attachments inherit their parent's status, so their parent must be readable too.
if ( 'attachment' === $post_type ) {
$condition .= " AND NOT ( {$wpdb->posts}.post_status = 'inherit' AND EXISTS ( SELECT 1 FROM {$wpdb->posts} AS acf_parent WHERE acf_parent.ID = {$wpdb->posts}.post_parent AND acf_parent.post_status IN ('$statuses') ) )";
}
$conditions[] = "( $condition )";
}
if ( count( $readable_post_types ) === count( $post_types ) ) {
return $where;
}
if ( $readable_post_types ) {
$readable_post_types = implode( "','", array_map( 'esc_sql', $readable_post_types ) );
$conditions[] = "{$wpdb->posts}.post_type IN ('$readable_post_types')";
}
return $where . ' AND (' . implode( ' OR ', $conditions ) . ')';
}