This question was posed on Twitter today …
Anyone know of a simple way to change a post to a page, while keeping the comments on that post in WordPress?
That, of course, got me thinking … “it can’t be THAT hard… I mean, pages and posts are essentially recorded the same way in the database. As long as I don’t change the post/page ID, the comments should still link back to it.”
Before we go any further, let me remind you to try this in a test environment and/or BACKUP YOUR SITE before attempting this. I always equate it back to Microsoft’s warning about the registry …
Using Registry EditorModifying the database incorrectly can cause serious, system-wide problems that may require you to re-install WindowsWordPress to correct them. MicrosoftWhoever is telling you to do this cannot guarantee that any problems resulting from the use of Registry Editordirectly modifying the database can be solved. Use this tool at your own risk.”
Now that we have the obligatory warnings out of the way, you have only yourself to blame.
I dug a little deeper and confirmed my thinking. Pages and posts are both stored in the posts table (i.e. wp_posts or whatever your table prefix is). The main difference is the “post_type” field. Posts have a post_type of “post” and pages have a post_type of “page” – who would’ve guessed it.
There was one other thing that I needed to add in the database, in the postmeta table (wp_postmeta or whatever your table prefix is). I had to create a new postmeta entry for the
post page to tell WordPress what page template to use for this page.
I won’t go into the specific about how to access the inner workings of MySQL, but I’ll provide sample SQL commands that’ll get the job done. Your hosting provider likely gives you access to phpMyAdmin or something similar… I suggest learning to use it. There are also WordPress plugins (I hear good things about Adminer) that will give you access to the database, but I’m not a fan of installing plugins for systems management tasks that I can do outside of WordPress (remember, SysAdmin by day).
Before modifying the database, we need to know which post ID we’ll be looking for:
- In your WordPress dashboard, click Posts.
- Click Edit for the post in question.
- Click the Get Shortlink button.
- In the link that’s displayed, take note of the part after the question mark – the number is the posts ID in the database (i.e. http://www.philerb.com/?p=012345 – the post ID is “012345”).
Now, in your database management tool (phpMyAdmin, MySQL Workbench, or – for the daring – the mysql command line), find the post in question:
- Get the row that corresponds to the post in question:
SELECT * FROM wp_posts WHERE ID="012345";
- Change the post_type to “page”:
UPDATE wp_posts SET post_type="page" WHERE ID="012345";
- Verify that there is no postmeta defining a page template for this post already (there shouldn’t be, but let’s play it safe):
SELECT * FROM wp_postmeta WHERE post_id="012345" AND meta_key="_wp_page_template";
- Add an entry to postmeta to define the page template:
INSERT INTO wp_postmeta (post_id,meta_key,meta_value) VALUES("012345","_wp_page_template","default")
Replace “default” with the shortname of the page template that you want to use.
That should do it – your post should now be a page, with the original comments intact.