Do posts, pages and / or custom post type objects have unique ID numbers or can there be multiple objects with the same IDs?

wordpress.stackexchange https://wordpress.stackexchange.com/questions/376691

  •  21-04-2021
  •  | 
  •  

Question

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

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top