How to Change Posts to Pages with the Custom Template in WordPress

   Back to list

Introduction

Posts in WordPress can be used in different ways because WordPress allows us to create custom post types. For various reasons, we may want to change them on pages. In this post, I will show you how to move your posts on pages as well as use them with our custom template using a PHP script.

The problem

The wp_posts table stores the content that we are interested in WordPress. There are posts, pages, and navigation menu items. To distinguish them, WordPress uses a column named post_type. For pages in the record, there is a ‘page’ value, for default posts ‘post’ and if we create our type of post, it saves the name of the type of post.

So we have to change record from ‘post’ (let’s assume that we change this type of posts) to ‘page’ for all of the posts that we are interested in.

To change the template, we will use a WordPress function add_post_meta, but before we do this, we need to take ID’s posts(which we want to change) from a database.

Solution

  1. Connect to the database.
  2. Take posts IDs which we need to change.
  3. Change posts on pages.
  4. Ascribe custom template to new pages.
  5. Disconnect with the database.

REMEMBER: You need to know how to deal with the WordPress database. Otherwise, you can destroy your data or worse – all page.

Ok! Let’s go:

1.Connect to the database

To connect to the database, we’ll use object mysqli.
Code:

$servername = "localhost";
$username = "root";
$password = "root";
$database = "your_WP_database";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{ // script will be here }

After connection with the database, we will write the main script.

2. Take posts IDs which we need to change.

Taking the Ids will be useful later for changing their template when they become pages. We will do it using a SQL statement “SELECT” and simple PHP ‘while’ loop.
Code:

$postsToChange = [];
$sql = "SELECT ID FROM wp_posts WHERE post_type = 'post’";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$i = 0;
while($row = $result->fetch_assoc()) {
$postsToChange[$i] = $row['ID'];
$i++;
}
}else{
echo "Error: " . $conn->error;
}

Great! Now, we have our ID’s in an array named postsToChange. So we can go to the next step.

3. Change posts on pages.

To change posts on the page, we will use SQL statement “UPDATE” with simple a command:

$sql = "UPDATE wp_posts SET post_type ='page' WHERE post_type = 'post'";

Now we need to make a request to database and check if everything went according to plan.

Code:

if ($conn->query($sql) === TRUE) {
echo "OK ! Posts are pages! “;
} else {
echo "Error: " . $conn->error;
}

Good job! We have just changed posts on pages.

4. Ascribe custom template to new pages.

Now we will use taken before ID’s to change page template. WordPress has a fancy function to do that – ‘add_post_meta’. We will use this function with ‘for’ loop to iterate an array with ID’s.

Code:

for($i = 0; $i < count($idsToChange); $i++)
{
add_post_meta($idsToChange[$i], '_wp_page_template', 'our-new-template.php');
}

For the third argument of a function, we only need to add the name of our new template, and that’s it.
We have the last step to take:

5. Disconnect with the database.

Here is only one line of code :

$conn->close();

Summary

In this post, I showed you how to change posts on pages as well as add to them our custom template quickly and easily. It should help you change your page structure if you need it. Remember that these are operations on the database, so if you are not sure of what you are doing, leave it to a programmer.

Send this to a friend