You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The ORM doesn't properly handle table prefixes when used with WordPress Multisite. The issue originates in the Database connection class which initializes the table prefix only once, causing all subsequent database operations to use incorrect prefixes when switching between blogs.
Observe that table names don't change with blog switching - for example, on blog 2 we still get wp_posts instead of wp_2_posts
Expected behavior
The ORM should respect WordPress's blog switching and use correct table prefixes for all database operations after switch_to_blog() is called.
Your setup
WordPress Version: 6.7.1
Module version: 4.0.0
Are you using framework?: Bedrock
Additional context
Possible solutions to consider:
Add WordPress hook listener in the Database class:
add_action('switch_blog', function($new_blog_id, $prev_blog_id) {
Database::$instance = null; // This will force new instance with updated prefix on next `getInstance()` call
});
Modify getTablePrefix() to always use current WordPress prefix:
Some tables like wp_users are shared across all sites in a Multisite network and should always use the base prefix ($wpdb->base_prefix) instead of site-specific prefixes. This also applies to other potential shared tables added by plugins.
This case could be handled by adding a $useBasePrefix property to models.
Thank you for creating this issue and adding as much detail as possible. I have never used WordPress with multisite, it's also for this reason that the library does not support multisite.
I will inquire about this part in order to fix bug and also add tests.
Unfortunately, I will not be able to fix the bug before January/February 2025. If you wish you can open a PR that I will look quickly to include it in the next version.
Describe the bug
The ORM doesn't properly handle table prefixes when used with WordPress Multisite. The issue originates in the Database connection class which initializes the table prefix only once, causing all subsequent database operations to use incorrect prefixes when switching between blogs.
Steps to reproduce the issue
wp_posts
instead ofwp_2_posts
Expected behavior
The ORM should respect WordPress's blog switching and use correct table prefixes for all database operations after
switch_to_blog()
is called.Your setup
Additional context
Possible solutions to consider:
getTablePrefix()
to always use current WordPress prefix:The text was updated successfully, but these errors were encountered: