Register custom fields to custom post type via ACF php

Question

I have created a CPT using this code

function cpt() {

$labels = array(
 'name'                => _x( 'CPT', 'Post Type General Name', 'twentytwenty' ),
 'singular_name'       => _x( 'CPT', 'Post Type Singular Name', 'twentytwenty' ),
 'menu_name'           => __( 'CPTS', 'twentytwenty' ),
 'parent_item_colon'   => __( 'Parent CPT', 'twentytwenty' ),
 'all_items'           => __( 'All CPTS', 'twentytwenty' ),
 'view_item'           => __( 'View CPT', 'twentytwenty' ),
 'add_new_item'        => __( 'Add New CPT', 'twentytwenty' ),
 'add_new'             => __( 'Add New', 'twentytwenty' ),
 'edit_item'           => __( 'Edit CPT', 'twentytwenty' ),
 'update_item'         => __( 'Update CPT', 'twentytwenty' ),
 'search_items'        => __( 'Search CPT', 'twentytwenty' ),
 'not_found'           => __( 'Not Found', 'twentytwenty' ),
 'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwenty' ),
 );
       
$args = array(
 'label'               => __( 'CPTS', 'twentytwenty' ),
 'description'         => __( 'CPT news and reviews', 'twentytwenty' ),
 'labels'              => $labels,
 'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
 'taxonomies'          => array( 'genres' ),
 'hierarchical'        => false,
 'public'              => true,
 'show_ui'             => true,
 'show_in_menu'        => true,
 'show_in_nav_menus'   => true,
 'show_in_admin_bar'   => true,
 'menu_position'       => 5,
 'can_export'          => true,
 'has_archive'         => true,
 'exclude_from_search' => false,
 'publicly_queryable'  => true,
 'capability_type'     => 'post',
 'show_in_rest' => true,

 );
register_post_type( 'cpt', $args );

}

I am using ACF to register custom fields with php. I need to add these fields programmatically.

This is the code for the custom fields:

if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array(
'key' => 'group_1',
'title' => 'My Group',
'fields' => array (
    array (
        'key' => 'field_1',
        'label' => 'Sub Title',
        'name' => 'sub_title',
        'type' => 'text',
    )
),
'location' => array (
    array (
        array (
            'param' => 'post_type',
            'operator' => '==',
            'value' => 'post',
        ),
    ),
),
));

endif;

I am able to register new fields to the default posts, but I am not able to register them to my custom post. Is there anything I should do?
Thank you

0
marissalianam 2 months 0 Answers 13 views 0

Leave an answer