1

i am developing in functions.php a function which, when a post in a certain category is created/edited, will create also a related page or update it if already exists.

The creation of a new page with wp_insert_post works like a charme, but if the page already exists (after else statement) and use wp_update_post instead, Wordpress returns just a blank screen and doesn't update the post and neither the page.

I can't figure out why, define('WP_DEBUG', true); doesn't return any hint.

$page_check = get_page_by_title($promo_page_title);
if(!isset($page_check->ID)){
    $new_page = array(
        'post_type' => 'page',
        'post_title' => $promo_page_title,
        'post_content' => $promo_page_content,
        'post_status' => 'publish',
        'post_author' => 1,
        'post_parent' => 27
    );
    $new_page_id = wp_insert_post($new_page);
    if($new_page_id){
        update_post_meta($new_page_id, '_wp_page_template', 'page_promo.php');
        $pids[0] = $new_page_id;
    } else {
        $e = TRUE;
        wp_die('Error creating promo page');
    }
} 
else {
    $new_page = array(
        'ID' =>  $page_check->ID,
        'post_type' => 'page',
        'post_title' => $promo_page_title,
        'post_content' => $promo_page_content,
        'post_status' => 'publish'
        'post_author' => 1,
        'post_parent' => 27
    );              
    $new_page_id = wp_update_post( $new_page );
    if($new_page_id){
        update_post_meta($new_page_id, '_wp_page_template', 'page_promo.php');
        $pids[0] = $new_page_id;
    } else {
        wp_die('Error updating promo page');
    }
}
unset($promo_page_content);

Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.