0

What are the canonical rules for the ID numbers of posts, pages and CPTs? Especially as they are created programatically.

1
  • All post types (pages, posts, CPT) are stored in the same wp_posts table, so they have unique ID.
    – nmr
    Commented Oct 17, 2020 at 18:08

1 Answer 1

2

The posts table, wp_posts is probably the most important table in the WordPress database but it does not contain only blog posts as its name implies. It stores various types of content including posts, pages, menu items, media attachments and any custom post types (CPT) that a site uses.

The ID column of this table is the unique number assigned to each content entry.

The table’s content nature is provided by the post_type column which denotes if the row is a post, page, attachment, nav_menu_item or another type, so none of above can be assigned the same ID by WordPress.

So unless you manually modify the database (which would be a bad move), ID's are unique within a blog/site.

Multisite

However, within a Wordpress Multisite Network, ID's are not unique. A post on one site can have the same ID as a post on another site, since each sub-site has a separate database/tables.

In such case you can use $post->guid instead of $post->ID. guid means Global Unique Identifier and this is Wordpress' tool for this exact purpose, to be unique in the entire network.

NB: The guid must be in lowercaps for it to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.