Support » Developing with WordPress » create new page first time a new post is saved

  • Resolved girl_number_5

    (@trutherone)


    Hi,
    As the tile suggests i want to create a new page after saving a new post for the first time. Its the standard Post type, not a custom post type. However its important that the post should have a specific ‘category’ so we only do this for posts of this post category. Heres some code I’ve got so far in my functions.php file :

    function create_newpage_for_newpost( $post_id, $post, $update ) {
        
    	// Only want to set if this is a new post!
        if ( $update ){
            return;
        }
         
        // Only set for post_type = post!
        if ( 'post' !== $post->post_type ) {
            return;
        }
         
        // Create new page based on this new post - New page should :
    	// 1. Use a pre-built elementor page-template
    	// 2. Use the post name (from post_id) in naming the new page.
    	// 3. pre populate some ACF fields with data from the post (advanced, so i'll find a method to do this myself)
        
    	$newpage_title     = get_the_title( $post_id ) . ' - history log';
        $link_back_to_post = get_permalink( $post_id );
    	
    }
    add_action( 'save_post', 'create_newpage_for_newpost', 10,3 );

    Its important that we only create one page per new post so we need to ignore ‘save to draft’ and other post statuses. The new page also should be based on a pre-saved Elementor page template (see comments in code above).

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,

    In you logical, I don’t see where you’re creating a new page. I think you should use https://developer.wordpress.org/reference/functions/wp_insert_post/ to create the page.

    Thread Starter girl_number_5

    (@trutherone)

    hi sebastienserre,
    Thats because thats where i am stuck! so i just commented where the proper code should be.I need to not just create a new page but it must use an Elementor page template – since they are different from the templates used in WP core.

    However for now ill just see if i can create a basic new WP page. Your link is for creating a post but i dont need to automate that .. when i manually create a post in wp admin i want a corresponding page with the custom naming convention in my code created too. I need the WordPress hook for creating a new page …

    Hello,

    Just in case, a Page is a post with the “page post_type”

    https://developer.wordpress.org/reference/functions/wp_insert_post/ is the function to create a post which have the post_type “page”

    
    $postarr = array(
        'post_type' = 'page',
    );
    $new_post_id = wp_insert_post( $postarr );
    // $new_post_id can be now use to attach a category
    

    About Elementor, you should check on their doc if there’s something to programatically assigne a template to a post/page

    Thread Starter girl_number_5

    (@trutherone)

    Hi,
    yes thanks – i think i figured it out with code from the official codex.

    Thank you.

    Great to hear.
    If your issue is solve, please mark this topic as solved.

    regards

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.