update_post_meta doesnt update template

I want to set Blank template but this function doesn’t do it.Whats the problem of this code?

register_activation_hook( __FILE__, 'eafw_activate_insert_post' );
function eafw_activate_insert_post() {
    //create a variable to specify the details of page
    $post = array(
        'post_name' => 'dneme1111',
        'post_content'   => 'This is a example content', //content of page
        'post_title'     =>'example title', //title of page
        'post_status'    =>  'publish' , //status of page - publish or draft
        'post_type'      =>  'woocommerceareas',  // type of post
    );
    wp_insert_post( $post ); // creates page
    if ( $post = get_page_by_path( 'dneme1111', 'woocommerceareas' ) ) {
        $id = $post->ID;
    }
    update_post_meta( $id, '_wp_page_template', 'Blank' );
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

What’s the problem of this code?

  1. You incorrectly called get_page_by_path() whereby if you want to specify the post type (the third parameter), then you need to specify the second parameter as well which is the return type and defaults to OBJECT. So:
    // Wrong: Missing the return type (second parameter).
    // Or I mean, the third parameter "becomes" the second here.
    get_page_by_path( 'dneme1111', 'woocommerceareas' )
    
    // Correct.
    get_page_by_path( 'dneme1111', OBJECT, 'woocommerceareas' )
    

    But then, if you just want to get the ID of the newly inserted post via wp_insert_post(), you don’t need to call get_page_by_path() and instead, you could simply do something like:

    if ( $id = wp_insert_post( $post ) ) {
        // call update_post_meta() here
    }
    
  2. Note that the value of the page template meta has to be a file name like template-Blank.php (see note [1] below) and not the name of the template like Blank as in Template Name: Blank. Secondly, the template has to be in the theme folder, so the file name should be relative to that folder.

    So for example, if you have two page templates, one in the root theme folder and the other in a subfolder:

    your-theme/
      index.php
      style.css
      template-Blank.php - page template in root theme folder
      foo/
        template-bar.php - page template in a subfolder
      ...

    Then you would set the page template like so:

    $id = 123; // post ID
    
    // Correct: Use the file name.
    update_post_meta( $id, '_wp_page_template', 'template-Blank.php' );
    // Wrong.
    update_post_meta( $id, '_wp_page_template', 'Blank' );
    
    // Assuming that the template has "Template Name: Bar".
    // Correct: Use the file name and relative subfolder path in this case (foo/).
    update_post_meta( 456, '_wp_page_template', 'foo/template-bar.php' );
    // Wrong.
    update_post_meta( 456, '_wp_page_template', 'Bar' );
    

And if you’re actually trying to override the template with a custom template stored in the wp-content/plugins directory, then you would want to use the template_include hook instead of the _wp_page_template metadata:

// Example to override the singular template for all posts of the woocommerceareas post type.
add_filter( 'template_include', function ( $template ) {
    return is_singular( 'woocommerceareas' ) ? '/path/to/your/template-name.php' : $template;
} );

Or yes, you could also programmatically copy the custom template to the active theme folder and then use the template metadata to override the page template..


Note [1] For the default page template (e.g. page.php), you can use default as the meta value.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x