Renaming post IDs – Okay to do?

Is it possible to change the post IDs within the wp_posts table without causing problems?

No it is not. If you change a posts ID, you then have to go through and fix other things:

  • the post meta key/value pairs would no longer have the correct ID
  • All terms in taxonomies such as categories and tags would no longer be attached to the post
  • Any nav menu links to the page would be broken
  • Post parent child relationships would be broken, including attachments on that page
  • Any caching mechanism would need flushing or problems would happen

It can be done, but it’s a significant amount of effort, and not worth the hassle

Generally, if you need to change the post ID of a post it’s a big warning that something you’re doing is not right, and that there are much easier solutions waiting to be found

To get around this problem, I am currently using a Switch to detect which language the page is in and then assigning the ID to a variable. With this information, I can then escape the URL using this ID.

If the entire purpose of this is to make the switch statement prettier I would strongly advise against this, not just because it’s ill advised, but because it won’t work. Multiple posts can’t have the same ID. But also because it’s unnecessary, and can be made nicer using some basic PHP

For example, you can bundle the case statements together:

    switch($language) {
        case "en_us":
        case "de_de":
            $downloads = 356;
            break;
        case "pt_pt": $downloads = 93; break;
        case "it_it": $downloads = 76; break;
        case "pt_br": $downloads = 59; break;
        case "fr_be": $downloads = 596; break;
        case "nl_be":
        case "es_es":
        case "fr_fr":
        case "es_mx":
        case "pl_pl":
            $downloads = 545;
            break;
    }

Or even use the default: case:

    switch($language) {
        case "en_us":
        case "de_de":
            $downloads = 356;
            break;
        case "pt_pt": $downloads = 93; break;
        case "it_it": $downloads = 76; break;
        case "pt_br": $downloads = 59; break;
        case "fr_be": $downloads = 596; break;
        default:
            $downloads = 545;
            break;
    }

Or better yet, use an array:

$languages = [
    "en_us" => 356,
    "de_de" => 356,
    "pt_pt" => 93,
    "it_it" => 76,
    "pt_br" => 59,
    "fr_be" => 596,
];
$downloads = 545;
if ( !empty( $languages[$language])) {
    $downloads = $languages[$language];
}

But this still leaves us with the fundamental anti-pattern:

The magic number problem

The code has hard coded post IDs. Should you ever decide to migrate, replace, import, etc, the code will be broken as the IDs may not be the same.

Instead, use post slugs, ideally that contain the language. Or better yet, use the APIs and features that MLP already provides and let the plugin do the work for you