Are all ID’s used unique?

I’m using JSON api plugin to pull data from wordpress into an app I am making. I need a unique identifier to make sure I don’t have any duplicates WHILE also using the identifier as a way to sort from newest post to oldest post. As of right now I’m confused because I pull the 10 most recent posts. I then take the ID and I attempt to reach example.com/?id=1774 but I don’t get the correct post. I get a different one. I’m starting to run out of ideas?

Related posts

2 comments

  1. If you look at the description for the different tables used by WordPress, the columns described as “auto_increment” will be unique.

    I am not sure what you are doing though. The URL structure you are using is not one that I recognize. That is, the syntax I think you want is this:

    example.com/?p=1774
    

    rather than this:

    example.com/?id=1774
    

    ?p= + a valid ID should always give you the post with the referenced ID number.

  2. Yes and No.

    1. Within a blog/site id‘s are unique. Unless they’ve been tampered with (by modifying the database’s AUTO_INCREMENT, or reusing old id‘s, which would be a bad move).

    2. Within a WordPress Multisite Network, id‘s are not unique. A blog post on one site can have the same id as a blog post on another site. Probably since each site has a separate database/table. (This just happened to me.)

    3. For third-party applications (like the app you are making) you shouldn’t use id as a globally unique identifier for a blog post, since if you pull data from different wordpress sites/blogs, then two blogs posts, from two different blogs, might have the same id (for the same reason as stated in point 2, above.), and you might end up getting the wrong blog post when doing a query in your own database.

    Instead: Use $post->guid (guid means Global Unique Identifier, and is WordPress’ tool for this purpose), and never change it, even if the URL or the domain name of the blog changes (it’s just an identifier, so it doesn’t need to always match the url). NB: guid must be in lowercaps for it to work.

    With the guid, you won’t be able to “use the identifier as a way to sort from newest post to oldest post.”, but you can pass the client_db_published_at date and use that instead (it is more reliable and detailed anyways):

    // When you already have the $post in question
    "blog_post[client_db_published_at]" => = get_the_time('c', $post->ID); // c  = ISO 8601 = 2004-02-12T15:19:21+00:00  . T is just a whitespace separator here. Ref: http://codex.wordpress.org/Formatting_Date_and_Time#Examples
    

Comments are closed.