Search code examples
phpwordpressadvanced-custom-fields

Advanced custom fields on two post type - wordpress


I need to add fields with php in two pages (in advanced custom fields plugin , wordpress)

Here is my try :

acf_add_local_field_group(array (
    'key' => 'songs_options',
    'title'      => 'songs options',
    'fields' => array (
        array (
            'key' => 'field_1',
            'label' => 'test title',
            'name' => 'test_field',
            'type' => 'text',
        ),
    ),
    'location' => array (
        array (
            array (
                'param' => 'post_type',
                'operator' => 'IN', 
                'value' => array( 'songs', 'videos'),  // i need a code like this line
            ),
        )
    ),
));

Solution

  • You should use the == operator with the locations, and add every location as a different array under 'location':

    register_field_group(array (
        'key'    => 'songs_options',
        'title'  => 'songs options',
        'fields' => array (
            array (
                'key'   => 'field_1',
                'label' => 'test title',
                'name'  => 'test_field',
                'type'  => 'text',
            ),
        ),
        'location' => array (
            array (
                array (
                    'param'    => 'post_type',
                    'operator' => '==', 
                    'value'    => 'songs',
                ),
            ),
            array (
                array (
                    'param'    => 'post_type',
                    'operator' => '==', 
                    'value'    => 'videos',
                ),
            ),
        ),
    ));