Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Insert corresponding page with every new post [WordPress] Re: Insert corresponding page with every new post [WordPress]

#123553
darrylm
Member

As usual, I’ve missed the point completely with WordPress when setting out to complete a task and have found instead of diving into the core there is a better way of doing it.

I have attempted all morning to untangle the functionality of post.php and related scripts but then had a brain wave and realised using a simple hook worked much more efficiently.

Below is the code inserted into the functions.php of my theme.

class np_service {
function add_page($post_id) {
$post = get_post($post_id);

// Create post object
$my_post = array(
‘post_title’ => $post->post_title,
‘post_name’ => $post->post_name,
‘post_content’ => $post->post_content,
‘post_status’ => ‘publish’,
‘post_type’ => ‘page’
);

// Insert the post into the database
wp_insert_post( $my_post );
}
}

add_action(‘publish_{custom_post_type}’, array(‘np_service’, ‘add_page’));