What are the canonical rules for the ID numbers of posts, pages and CPTs? Especially as they are created programatically.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0