• Hi everyone

    We are about to relaunch a Website with wordpress.
    So far the old page provided an API with events. Every event had a unique identifier ID (which is adding up, currently around 31000)

    On our new page we want to prevent problems with duplicate IDs and would like to either manipulate the post ID, to make our first post (event) ID start from let’s say 40000, or to set a second, custom post ID, which also increases by 1 for every new post.

    I searched the web and the forum but could not find anything.
    Does somebody have an idea how to accomplish this task?

    As a fallback solution I also thought about letting the post ID be whatever it is (default, starting from 1), but in our API we just add 40000 to the ID and generate an «artificial» ID, whose only purpose is to be higher than 31000 and only appears in our API.

    Here is an example of the API JSON, just as a reference:

    {
      "31424": {
        "field_event_origin_id": "31424",
        "changed": 1524129659,
        "title": "Christian L\u00f6ffler (live)",
        "field_event_subtitle": "Club: Electronica, pr\u00e4sentiert mit h\u00f6rperlen.",
        "field_event_image": "http:\/\/www.mypage.ch\/sites\/default\/files\/image.jpg",
        "body": "Text Text text",
        "field_event_date": 1537560000,
        "field_event_doors": 1537560000,
        "field_event_doors_string": "22:00",
        "field_event_doors_datetime": "2018-09-21T22:00:00",
        "field_event_ticket_link": "https:\/\/www.linktoticket.com",
        "field_event_origin_link": "http:\/\/www.mypage.ch\/#31424",
        "field_event_youtube_clip_id": "ee9nxjua30U",
        "field_event_soundcloud_clip_id": null,
        "field_event_vimeo_clip_id": null,
        "field_event_price": "CHF 25",
        "field_event_facebook_event_id": null
      },
      "31426": {
        "field_event_origin_id": "31426",
        "changed": 1524129659,
        "title": "Sommerfest - mit Shahin Najafi",
        "field_event_subtitle": "Grosse Halle: iranischer Pop-Rock, Worldpol, pr\u00e4sentiert mit Verein Yari",
        "field_event_image": "http:\/\/www.mypage.ch\/sites\/default\/files\/Sommerfest_18_digi2_0.png",
        "body": "Text Text text",
        "field_event_date": 1529780400,
        "field_event_doors": 1529780400,
        "field_event_doors_string": "21:00",
        "field_event_doors_datetime": "2018-06-23T21:00:00",
        "field_event_ticket_link": "https:\/\/www.linktoticket.com",
        "field_event_origin_link": "http:\/\/www.mypage.ch\/#31426",
        "field_event_youtube_clip_id": "hX4vBjFH2sI",
        "field_event_soundcloud_clip_id": null,
        "field_event_vimeo_clip_id": null,
        "field_event_price": "CHF 30",
        "field_event_facebook_event_id": null
      },
    }

    (Just two events, should be enough…)

    Thanks for your help. Much appreciated.

    Cheers

Viewing 5 replies - 1 through 5 (of 5 total)
  • If it was me doing it, I’d set up a new database table with two columns – one for the post ID from the post, and an auto-incrementing primary ID for the event ID. Then set the auto increment to start at 40,000. When a new event is created, you can call the wp_insert_post action to set a new record in that database table. This lets you keep a defined record of every post, keep an auto-incremented ID number that won’t be used for anything else, and lets you keep the standard WordPress post ID for the post objects so that nothing goes astray.

    Thread Starter nr2012

    (@nr2012)

    Hm… Thank you for your answer!
    Would this also work if I still want to use the WP REST API?
    It sounds like this second database table is somehow disconnected from WP, and maybe the REST API does not have access to that?

    Or would this mean that when I write a custom endpoint I will have to get that table and merge it together with the actual event data?

    Cheers

    As far as I know adding a new post via the Rest API will call the same actions as adding apost through the admin area. It wouldn’t make sense to me if it didn’t. So, I’d say that I’d be almost sure that it will work like that, but I’d try some testing to see, just in case.

    Thread Starter nr2012

    (@nr2012)

    Ah no sry, I think you misunderstood. But thanks anyway!!!
    I am just not sure if the endpoint is retrieving the additional ID as well and can show in on the API.

    Because just calling the WP REST API for the posts will definitely show the posts (events) data, but how will it get the additional ID.

    I guess I have to connect to that table and retrieve the custom ID depending on the post’s ID, is that right?

    Sorry for being a it slow here.

    Cheers

    OK, I understand that part now.

    To be honest I haven’t worked with the Rest API yet, so I’m not sure. I would think that there will be some sort of filer or action that will let you add that in. It’s worth looking through the documentation to see what’s available.

    https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Set a custom consecutive post ID’ is closed to new replies.