Generate dynamic page from custom post type

I've brought this up in past support questions but am hoping I can clarify more here.

I have a custom post type and while there are posts, I would like each post to generate a dynamic page with the same post content but styled differently. The admin doesn't need the ability to edit the dynamic page at all as it's just another way to showcase the gallery so I don't think I need an additional plugin or new custom post template, it can just be created within the custom post. Thanks!

  • Adam Czajczyk

    Hello Kate,

    I hope you're well today and thank you for your question!

    I think one of our developers or 2nd-line support guys may be of help here as what you're trying to achieve (if I understand it correctly) is a kind of "virtual pages" which we're also using in some of our plugins. Let me then ask them for an advice :slight_smile:

    Please note however that their response time may be a bit longer than ours here on support forum as they have to deal with a lot of complex issues on daily basis. I'm sure though they'll find a moment to share their experience with you.

    That said, please keep an eye on this thread for further reference!

    Best regards,
    Adam

  • Kate Eldridge

    Here is what I'm working with right now found here on Tom McFarlin's site

    function programmatically_create_post(){
    	//Initialize the page ID to -1. This indicates no action has been taken.
    	$post_id = -1;
    
    	// Setup the author, slug, and title for the post
    	$author_id = 1;
    	$slug = '$title-full-gallery';
    	$title = '$title-full-gallery';
    
    	// If the page doesn't already exist, then create it
    	if( null == get_page_by_title( $title ) ) {
    
    		// Set the post ID so that we know the post was created successfully
    		$post_id = wp_insert_post(
    			array(
    				'comment_status'	=>	'closed',
    				'ping_status'		=>	'closed',
    				'post_author'		=>	$author_id,
    				'post_name'		=>	$slug,
    				'post_title'		=>	$title,
    				'post_status'		=>	'publish',
    				'post_type'		=>	'post'
    			)
    		);
    
    	// Otherwise, we'll stop
    	} else {
    
        		// Arbitrarily use -2 to indicate that the page with the title already exists
        		$post_id = -2;
    
    	} // end if
    
    } // end programmatically_create_post
    
    add_filter( 'after_setup_theme', 'programmatically_create_post' );

    And then calling it in my single.php to generate a new post every time the post is created

    $post_id = programmatically_create_post()
    if( -1 == $post_id || -2 == $post_id ) {
       // The post wasn't created or the page already exists
    }

    Haven't put it in my code yet, but think it should do the trick, but need to then create a layout with in single.php for this new post. Not quite sure on that yet.

  • Predrag Dubajic

    Hi Kate,

    Apologies for the delay on this thread, our developers are quite busy lately, we usually try to help with all kind of issues in the forums but custom coding questions are not having priority right now, the question is still active but I can't give you any info on when developers will be able to assist you with this.

    If you're in a hurry to get this sorted you might want to post a job in our job board where you can hire a developer to assist further:
    https://premium.wpmudev.org/wpmu-jobs/
    Please note that, no WPMU official staff members are allowed to work in the job board.

    Best regards,
    Predrag

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.