• Using wp_insert_post (https://codex.www.ads-software.com/Function_Reference/wp_insert_post) I can set a value for the page_template key:
    ‘page_template => [ <template file> ] //Sets the template for the page.

    However, I’ve not been able to do this successfully.

    I want to use a custom theme page (page-proposal.php) in the theme directory properly named as “Page Proposal”. My custom form uses wp_insert_post as follows (in part):

    $prop_page = array();

    $prop_page[‘page_template’] = ‘page-proposal.php’;

    wp_insert_post($prop_page);

    Though everything else in the array goes through fine, the new page’s template ends up as “default”. I checked the db and found the entries in the wp_postmeta table ending up as

    _wp_page_template default

    When I change the template through the GUI everything’s OK, and the db updates properly. This makes me think that my wp_insert_post() isn’t sending the page_template value to the right place.

    Ideas?

Viewing 1 replies (of 1 total)
  • Not sure if it’s gonna help you now. But the issue is still there probably the admin includes are not completely done when you’re at the theme levels or for that matter page template level.

    Make sure that you set the post_type and post_parent correctly.

    Do not set the page_template now.

    $my_post_id = wp_insert_post($prop_page);
    
    if($my_post_id) {
      update_post_meta($my_post_id, '_wp_page_template',  'page-proposal.php');
    }

    should do the trick.

Viewing 1 replies (of 1 total)
  • The topic ‘Trouble with page_template key for wp_insert_post’ is closed to new replies.