1

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

I ask this because I have a WordPress Multisite in which I separate the sites by language. Unfortunately, the IDs for each language are distinct, making the linking a bit complex.

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.

    $language = strtolower(mlp_get_current_blog_language(false));
    switch($language) {
        case "en_us": $downloads = 356; break;
        case "pt_pt": $downloads = 93; break;
        case "it_it": $downloads = 76; break;
        case "pt_br": $downloads = 59; break;
        case "nl_be": $downloads = 545; break;
        case "fr_be": $downloads = 596; break;
        case "de_de": $downloads = 356; break;
        case "es_es": $downloads = 545; break;
        case "fr_fr": $downloads = 545; break;
        case "es_mx": $downloads = 545; break;
        case "pl_pl": $downloads = 545; break;
    }
<a href="'; echo esc_url( get_page_link( $downloads ) ); echo'">

However, if I could simply rename the IDs to each of the "downloads" pages to say, "545", then I would not need to go through this switch.

Any thoughts? Would this break something else down the line?

*Note, the IDs are unique within their own table. There are 11 tables.

*Note that the titles are different throughout as they will match the target language (telechargement, descarga, etc.). So, I can't use this either.

Thanks!

Brad

  • You don't need to repeat the downloads bit repeatedly like that, you can bundle case statements together. Eitherway, changing post IDs is a great way to introduce unexpected bugs and odd behaviour, and generally not a good idea ( e.g. it would immediately break all post meta and term relationships ). Hardcoding post IDs in code is not a good idea either – Tom J Nowell Jan 16 at 16:16
3

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

  • 1
    Thanks @TomJNowell for your reply. Your absolutely right. I should avoid using the post IDs and your last comment was crucial for that. Now I'll look and see if I can make a nice script for finding the ID from the wp_multilingual_linked table or see if there is some sort of built in script. Thanks again! – AlmostPitt Jan 16 at 16:37
  • Post slugs ftw ^_^ – Tom J Nowell Jan 16 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.