Creating multiple page URL without creating the pages in WordPress

I want to create many page URL without actually creating the pages in the WordPress dashboard.
And assigned a particular template.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This is a simple outline so don’t use this in production but it’s a working example.

Define your template and place it in templates/template-portfolio-template.php inside your theme.

<?php
/*
 * Template Name: Portfolio Template
 * Description: Page template to display portfolio custom post types
 */

echo 'This is the Portfolio Template!';

Next, you’ll need to programmatically generate the posts.

function programmatically_create_post($post_type = 'post', $title, $content, $template_rel_path='')
{
    // Initialize the page ID to -1. This indicates no action has been taken.
    $post_id = -1;

    // Prep
    $author_id = get_current_user_id();
    $title = wp_strip_all_tags( $title ); // remove any junk
    $slug = sanitize_title ($title); // converts to a usable post_name
    $post_type = post_type_exists($post_type) ? $post_type : 'post'; // make sure it exists

    // 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(
          'post_name'           =>  $slug,
          'post_title'          =>  $title,
          'post_content'        =>  $content,
          'post_type'           =>  $post_type,
          'post_author'         =>  $author_id,
          'comment_status'      =>  'closed',
          'ping_status'         =>  'closed',
          'post_status'         =>  'publish',
          )
        );

    if( $post_id && $post_id > 0 && ! empty( $template_rel_path)) {

      // make sure the template exists
      $template_full_path = trailingslashit( get_stylesheet_directory() ) . $template_rel_path;
      if( file_exists( $template_full_path )) {

         // set the post meta data -- use relative path
         update_post_meta( $post_id, '_wp_page_template', $template_rel_path );
      }
    }

    // Otherwise, we'll stop
    } else {
            // Arbitrarily use -2 to indicate that the page with the title already exists
            $post_id = -2;

    } // end if

  return $post_id;

} // end programmatically_create_post

Then test that it works w/or without a template.

add_action( 'init', function(){

  $template = 'templates/template-portfolio-template.php';

  $post_id_temp = programmatically_create_post( 'page', 'My Page With a Template', 'This is some sample content for the template! ' . timer_stop(4), $template );

  $post_id_no_temp = programmatically_create_post( 'page', 'My Page Without A Template', 'This is some sample content! ' . timer_stop(4), '' );

  if( -1 == $post_id_temp || -2 == $post_id_temp ) { // The post wasn't created or the page already exists
     //...
  } else {
     $post = get_post($post_id_temp);
     $url = get_permalink($post->ID); // your new page url
  }

  if( -1 == $post_id_no_temp || -2 == $post_id_no_temp ) { // The post wasn't created or the page already exists
     //...
  } else {
     $post = get_post($post_id_no_temp);
     $url = get_permalink($post->ID); // your new page url 
  }

} );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x