Bug during post insertion on a multilingual multisite

Hello everyone!

I’m reaching out to you because today, I’m a bit lost. I’ve tried everything I can think of and I can’t seem to figure it out…

I have a WordPress multisite with Sage + Bedrock. I’m using Polylang for translation management.

On the main site, I have no issues. However, on the secondary site with the following code :

    $current_blog_id = get_current_blog_id();
    switch_to_blog(2);
    $args = [        'post_type' => 'my_cpt',        'post_title' => 'Test',        'post_status' => 'draft',    ];
    $post_id = wp_insert_post($args);
    if($post_id instanceof WP_Error) {
        error_log($post_id->get_error_message());
    } else {
        error_log("Post inserted with ID: " . $post_id);
    }
    restore_current_blog($current_blog_id);

I have a major issue. When I insert a custom post type, my homepage for both FR and EN “jumps” on the secondary site. And the post is inserted without any errors.

I am forced to either for repear :

  • Update a post
  • Reconfigure the FR and EN homepages

I’ve tried many things:

  • Deactivating all plugins
  • Reinstalling Polylang
  • Trying a different method of insertion than wp_insert_post
  • Identifying any changes in the theme or behavior of insertion
  • Nothing in terms of debugging, not even a warning.
  • Checked for any conflicts in terms of slugs.

And the problem remains the same. Does anyone have a solution? I’m starting to run out of ideas… :frowning:

Thanks in advance,
Mathieu

Not sure if your issue is related to Bedrock or Sage. But restore_current_blog() doesn’t take any arguments. So maybe your call to that function bugs out and causes you to end up on site 2.

If this doesn’t help I think you need to tell us what hook you are using to execute the code.

1 Like

Hello,

Thank you very much for your feedback. Indeed, originally I did not include any parameters in the function. It was during multiple tests that I added them. I tried removing it completely, adding nothing, and then adding a parameter, but the bug persists.

To provide context, the code is executed within a plugin via WP_CLI, like this:

public static function CLI()
{
    if (class_exists('WP_CLI')) {
        \WP_CLI::add_command('init-my-custom-post', function ($args, $assoc_args) {
            self::myFunctionName();
            \WP_CLI::success('Import finished ;)');
        });
    }
}

And the function:

public function myFunctionName()
{

    switch_to_blog(2);
    $args = [
        'post_type' => 'wetix-shop',
        'post_title' => 'Test',
        'post_status' => 'draft',
    ];
    $post_id = wp_insert_post($args);
    if($post_id instanceof WP_Error) {
        error_log($post_id->get_error_message());
    } else {
        error_log("Post inserted with ID: " . $post_id);
    }
}

Normally, this is a plugin that retrieves a feed and performs an insertion. However, during debugging, I noticed that it bugs at this level.

I tried placing the small insertion directly in the functions.php file and there I have no problem.

Could this provide you with any clues?

Note : I found an alternative, which involves not using WP_CLI but rather a curl command with a hard-coded GET token to execute the insertion. It works. I still find it very strange, especially since I have no errors at all, and I even tried running wp init-my-custom-post --debug but got nothing. I am open to solutions :slight_smile:

Thank you in advance,
Mathieu

Like @folbert mentioned, I don’t think this has anything to do with Bedrock or Sage?

You might want to try asking over at a place like https://wordpress.stackexchange.com/