Site icon Hip-Hop Website Design and Development

how to get the post ID from wp_insert_post

I am programatically inserting a new page, then I want to use the ID of that new page to add a template, however wp_insert_post isn’t returning me any ID, here’s the code I am using

$tittle = 'My new page';
$newPage = array(
        'post_type'     => 'page',
        'post_title'    => $tittle,
        'post_status'   => 'publish'
    );
$post_id = wp_insert_post( $newPage);
print_r($post_id);

in reallity I use the code this way

function criar_pag($titulo, $template = 0){
    $a = get_page_by_title( $titulo, 'post' );
    if ($a == null || $a->post_status == 'trash') {
        $pagina = array(
            'post_type'     => 'page',
            'post_title'    => $titulo,
            'post_status'   => 'publish'
        );
        $post_id = wp_insert_post( $pagina);
        update_post_meta($post_id, '_wp_page_template', $template);
    }
}
criar_pag("my new page", "customtemplate.php");