Skip to main content
deleted 4 characters in body
Source Link
Adam
  • 16.5k
  • 1
  • 44
  • 62

This is how you do it...

function update_post_template($post_id, $post){

    global $post;

    //set $post_parent_id however you wish  

    if ($post->post_parent == $post_parent_id) {
    
        //unhook save_post action to avoid infinite loop
        remove_action('save_post', 'update_post_template'); 

        //change template
        update_post_meta( $post->ID$post_id, '_wp_page_template', 'custom_template.php' );
   
        //re-hook save_post action
        add_action('save_post', 'update_post_template');

    }
}

add_action('save_post', 'update_post_template', 10, 2);

Notes:

To explain what is happening, first we hook onto the save_post action which is fired when inserting/updating a post. We then check for the existence of a certain post_parent id which is up to you to decide and determine which (you may want to extend this conditional statement to cover multiple cases). If we find a match, we unhook the save_post action to avoid creating an infinite loop when we call our update_post_meta function. Once the page template has been updated, we re-hook the save_post action and continue on our merry way.

This is how you do it...

function update_post_template(){

    global $post;

    //set $post_parent_id however you wish  

    if ($post->post_parent == $post_parent_id) {
    
        //unhook save_post action to avoid infinite loop
        remove_action('save_post', 'update_post_template'); 

        //change template
        update_post_meta( $post->ID, '_wp_page_template', 'custom_template.php' );
   
        //re-hook save_post action
        add_action('save_post', 'update_post_template');

    }
}

add_action('save_post', 'update_post_template');

Notes:

To explain what is happening, first we hook onto the save_post action which is fired when inserting/updating a post. We then check for the existence of a certain post_parent id which is up to you to decide and determine which (you may want to extend this conditional statement to cover multiple cases). If we find a match, we unhook the save_post action to avoid creating an infinite loop when we call our update_post_meta function. Once the page template has been updated, we re-hook the save_post action and continue on our merry way.

This is how you do it...

function update_post_template($post_id, $post){

    //set $post_parent_id however you wish  

    if ($post->post_parent == $post_parent_id) {
    
        //unhook save_post action to avoid infinite loop
        remove_action('save_post', 'update_post_template'); 

        //change template
        update_post_meta( $post_id, '_wp_page_template', 'custom_template.php' );
   
        //re-hook save_post action
        add_action('save_post', 'update_post_template');

    }
}

add_action('save_post', 'update_post_template', 10, 2);

Notes:

To explain what is happening, first we hook onto the save_post action which is fired when inserting/updating a post. We then check for the existence of a certain post_parent id which is up to you to decide and determine which (you may want to extend this conditional statement to cover multiple cases). If we find a match, we unhook the save_post action to avoid creating an infinite loop when we call our update_post_meta function. Once the page template has been updated, we re-hook the save_post action and continue on our merry way.

added 1 characters in body
Source Link
Adam
  • 16.5k
  • 1
  • 44
  • 62

This is how you do it...

function update_post_template($post){

    global $post;

    //set $post_parent_id however you wish  

    if ($post->post_parent == $post_parent_id) {
    
        //unhook save_post action to avoid infinite loop
        remove_action('save_post', 'update_post_template'); 

        //change template
        update_post_meta( $post_id$post->ID, '_wp_page_template', 'your_custom_template''custom_template.php' );
   
        //re-hook save_post action
        add_action('save_post', 'update_post_template');

    }
}

add_action('save_post', 'update_post_template');

Notes:

To explain what is happening, first we hook onto the save_post action which is fired when inserting/updating a post. We then check for the existence of a certain post_parent id which is up to you to decide and determine which (you may want to extend this conditional statement to cover multiple cases). If we find a match, we unhook the save_post action to avoid creating an infinite loop when we call our update_post_meta function. Once the page template has been updated, we re-hook the save_post action and continue on our merry way.

This is how you do it...

function update_post_template($post){

    //set $post_parent_id however you wish  

    if ($post->post_parent == $post_parent_id) {
    
        //unhook save_post action to avoid infinite loop
        remove_action('save_post', 'update_post_template'); 

        //change template
        update_post_meta( $post_id, '_wp_page_template', 'your_custom_template' );
   
        //re-hook save_post action
        add_action('save_post', 'update_post_template');

    }
}

add_action('save_post', 'update_post_template');

Notes:

To explain what is happening, first we hook onto the save_post action which is fired when inserting/updating a post. We then check for the existence of a certain post_parent id which is up to you to decide and determine which (you may want to extend this conditional statement to cover multiple cases). If we find a match, we unhook the save_post action to avoid creating an infinite loop when we call our update_post_meta function. Once the page template has been updated, we re-hook the save_post action and continue on our merry way.

This is how you do it...

function update_post_template(){

    global $post;

    //set $post_parent_id however you wish  

    if ($post->post_parent == $post_parent_id) {
    
        //unhook save_post action to avoid infinite loop
        remove_action('save_post', 'update_post_template'); 

        //change template
        update_post_meta( $post->ID, '_wp_page_template', 'custom_template.php' );
   
        //re-hook save_post action
        add_action('save_post', 'update_post_template');

    }
}

add_action('save_post', 'update_post_template');

Notes:

To explain what is happening, first we hook onto the save_post action which is fired when inserting/updating a post. We then check for the existence of a certain post_parent id which is up to you to decide and determine which (you may want to extend this conditional statement to cover multiple cases). If we find a match, we unhook the save_post action to avoid creating an infinite loop when we call our update_post_meta function. Once the page template has been updated, we re-hook the save_post action and continue on our merry way.

added 560 characters in body
Source Link
Adam
  • 16.5k
  • 1
  • 44
  • 62

This is how you do it...

function update_post_template($post){

    //set $post_parent_id however you wish  

    if ($post->post_parent == $post_parent_id) {
    
        //unhook save_post action to avoid infinite loop
        remove_action('save_post', 'update_post_template'); 

        //change template
        update_post_meta( $post_id, '_wp_page_template', 'your_custom_template' );
   
        //re-hook save_post action
        add_action('save_post', 'update_post_template');

    }
}

add_action('save_post', 'update_post_template');

Notes:

To explain what is happening, first we hook onto the save_post action which is fired when inserting/updating a post. We then check for the existence of a certain post_parent id which is up to you to decide and determine which (you may want to extend this conditional statement to cover multiple cases). If we find a match, we unhook the save_post action to avoid creating an infinite loop when we call our update_post_meta function. Once the page template has been updated, we re-hook the save_post action and continue on our merry way.

This is how you do it...

function update_post_template($post){

    //set $post_parent_id however you wish  

    if ($post->post_parent == $post_parent_id) {
    
        //unhook save_post action to avoid infinite loop
        remove_action('save_post', 'update_post_template'); 

        //change template
        update_post_meta( $post_id, '_wp_page_template', 'your_custom_template' );
   
        //re-hook save_post action
        add_action('save_post', 'update_post_template');

    }
}

add_action('save_post', 'update_post_template');

This is how you do it...

function update_post_template($post){

    //set $post_parent_id however you wish  

    if ($post->post_parent == $post_parent_id) {
    
        //unhook save_post action to avoid infinite loop
        remove_action('save_post', 'update_post_template'); 

        //change template
        update_post_meta( $post_id, '_wp_page_template', 'your_custom_template' );
   
        //re-hook save_post action
        add_action('save_post', 'update_post_template');

    }
}

add_action('save_post', 'update_post_template');

Notes:

To explain what is happening, first we hook onto the save_post action which is fired when inserting/updating a post. We then check for the existence of a certain post_parent id which is up to you to decide and determine which (you may want to extend this conditional statement to cover multiple cases). If we find a match, we unhook the save_post action to avoid creating an infinite loop when we call our update_post_meta function. Once the page template has been updated, we re-hook the save_post action and continue on our merry way.

Source Link
Adam
  • 16.5k
  • 1
  • 44
  • 62
Loading