xxx2coder
@xxx2coder
веб-разработчик

Как добавить ACF-поле к типам записей?

Есть тип записи - квиз. Не могу добавить поле выбора квиза ко всем зарегистрированным типа постов, добавляется только к post, page. Как быть?
Вот код
$postTypesObj = get_post_types( array(), 'objects' );

foreach ( $postTypesObj as $index => $postType ) {
    if ( $postType->public == true && $postType->name != 'attachment') {
        if ( function_exists( 'acf_add_local_field_group' ) ) {
            acf_add_local_field_group( array (
                'key' => 'quizzes_select_' . $postType->name,
                'title' => 'Квиз',
                'fields' => array (
                    array (
                        'key' => 'quizzes_select__input',
                        'label' => 'Выберите квиз',
                        'name' => 'quizzes_select',
                        'type' => 'post_object',
                        'prefix' => '',
                        'instructions' => '',
                        'post_type' => 'quizzes',
                        'taxonomy' => '',
                        'required' => 0,
                        'return_format' => 'id',
                        'conditional_logic' => 0,
                        'wrapper' => array (
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => '',
                        'placeholder' => '',
                        'prepend' => '',
                        'append' => '',
                        'maxlength' => '',
                        'readonly' => 0,
                        'disabled' => 0,
                    )
                ),
                'location' => array (
                    array (
                        array (
                            'param' => 'post_type',
                            'operator' => '==',
                            'value' => $postType->name,
                        ),
                    ),
                ),
                'menu_order' => 0,
                'position' => 'normal',
                'style' => 'default',
                'label_placement' => 'top',
                'instruction_placement' => 'label',
                'hide_on_screen' => '',
            ) );
        }
    }
}

второй вариант
$locations = array();
$postTypesObj = get_post_types( array(), 'objects' );
$i = 0;
foreach ( $postTypesObj as $postType ) {
    if ( $postType->public == true && $postType->name != 'attachment') {
        $locations[$i] = 
                    array (
                        array (
                            'param' => 'post_type',
                            'operator' => '==',
                            'value' => $postType->name,
                        ),
                    );
        $i++;
    }
}
if ( function_exists( 'acf_add_local_field_group' ) ) {
    acf_add_local_field_group( array (
        'key' => 'quizzes_select' ,
        'title' => 'Квиз',
        'fields' => array (
            array (
                'key' => 'quizzes_select__input' ,
                'label' => 'Выберите квиз',
                'name' => 'quizzes_select',
                'type' => 'post_object',
                'prefix' => '',
                'instructions' => '',
                'post_type' => 'quizzes',
                'taxonomy' => '',
                'required' => 0,
                'return_format' => 'id',
                'conditional_logic' => 0,
                'wrapper' => array (
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ),
                'default_value' => '',
                'placeholder' => '',
                'prepend' => '',
                'append' => '',
                'maxlength' => '',
                'readonly' => 0,
                'disabled' => 0,
            )
        ),
        'location' =>  $locations,
        'menu_order' => 0,
        'position' => 'normal',
        'style' => 'default',
        'label_placement' => 'top',
        'instruction_placement' => 'label',
        'hide_on_screen' => '',
    ) );
}

Результат у обоих вариантов одинаковый
  • Вопрос задан
  • 107 просмотров
Решения вопроса 1
xxx2coder
@xxx2coder Автор вопроса
веб-разработчик
Решил проблему. Дело было в том, что поля регистрируются слишком рано. Нашёл запрос в бд на отбор типов постов
P.s. чуть позже скину код
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы