Update Post Types with SQL Syntax

Sometimes when you’re first starting out with WordPress, your wires can get crossed when it comes to pages and posts.

People start out making pages or posts playing around, which leads to actually making web pages and blog posts for more serious purposes. Although… most people aren’t really sure the difference between the two, because posts and pages seem pretty darn similar if you think about it.

..and they are … except they’re not..

they have slightly different functionality, but of course you realized that a little too late after you already started to build out your site. AND NOW WHAT!!!??? and now you wish your pages were posts and your posts were pages and you want to switch them around!

Ok maybe not but it’s useful to know how to change post types anyway.

First though, let’s go over some of the differences between pages and posts:

  • Posts have a time stamp and are displayed chronologically, while Pages are top level by default regardless of creation date.
  • Posts are organized with categories and tags, while Pages are organized in a hierarchy with parent and child menu pages.
  • Posts display author and published date by default while Pages are treated as static content with no author or publish date.

If you want to read more about it, WPBeginner has a great article on it here.

How to Update post_type with SQL Syntax

In a WordPress install, post types are saved in the post_type column of the wp_posts table of the database. Even though pages and posts are different, they share a happy home in the _posts database table.

Don’t know what a database is? Please, to go here.

You may want to check out how to use SSH, how to connect to SQL via wp-cli, and some SQL Basics first too.

OK. Assuming we’re all up to speed now, and we’re in our terminals connected to SQL (that escalated quickly).

Let’s first locate the post you wish to change by post id:

select * from wp_posts where ID=37;

Of course, making sure you’re using the correct prefix for your database, and the ID applicable to your target post or page.

You can locate the page or post ID in the browser URL when editing the page/post:

Or, alternately you can locate the page/post using the post title:

select * from wp_posts where post_name = postnamehere;

Or, if you want to convert all pages to posts (or vise verse) you can select by the post_type:

select * from wp_posts where post_type  = 'page';

Now I feel we’ve got a good grasp on the options for selects. Let’s update!

Oh before we do this, just want to remind you to either work on a dev/staging site or at least take a back up before making changes directly to a database using SQL syntax.

We’re good at what we do but don’t get brave (>.>)

She says …. as she proceeds to do the very thing she just said not to do.

Image result for monkey meme

Alright so I think we got the point across here. Or did we? These are confusing times. Anyway — Update statements!

UPDATE statements. Here is how to change the post type (from ‘page’ or something else) to ‘post’ for the post with an ID of 37:

UPDATE wp_posts SET post_type = 'post' where ID=37;

Of course you can change the WHERE clause to something else, like: post_name = postnamehere

UPDATE wp_posts SET post_type = 'post' where post_name= 'win';

Or if you want to convert a post to a page you can do dat too:

UPDATE wp_posts SET post_type = 'page' where ID=37;

That’s it. It’s a simple spell but quite unbreakable.

Now you’ll want to push your changes to live. 😛

Leave a Comment