custom post types – Call to a member function add_rewrite_tag() on null on register_post_type

Question

Im trying to register a post type using the class below, the problem that appears is a fatal error “Call to a member function add_rewrite_tag() on null”. I did some research and the general consensus was that i needed to put the function in a hook (init) within an add_action, which i have done within the construct of the class. But i still get the error.

Any help would be greatly appreciated.

class PostTypes { 


public function __construct()
{
    add_action( 'init', $this->registerCustomers() );
}

public function registerCustomers() {

    $labels = [
        'name'                  => esc_html( 'Customers' ),
        'singular_name'         => esc_html( 'Customer' ),
        'all_items'             => esc_html( 'All Customers' ),
        'featured_image'        => esc_html( 'Thumbnail Image' ),
        'set_featured_image'    => esc_html( 'Set Thumbnail Image' ),
        'remove_featured_image' => esc_html( 'Remove Thumbnail Image' ),
    ];
    $args = [
        'labels'       => $labels,
        'public'       => true,
        'show_in_rest' => true,
        'has_archive'  => false,
        'rewrite'      => [
            'slug'       => 'customers',
            'with_front' => false,
        ],
        'supports'     => [
            'title',
            'editor',
            'excerpt',
            'thumbnail',
            'post-formats',
            'custom-fields',
        ],
        'menu_position'       => 10,
    ];

     
    register_post_type( 'customer', $args );
}

}

$postTypes = new PostTypes();

0
user2955824 5 months 2022-06-29T07:25:33-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse