#48878 closed defect (bug) (invalid)
5.1 wp_insert_site bug
Reported by: | bikecrazyy | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 5.1 |
Component: | Networks and Sites | Keywords: | |
Focuses: | multisite | Cc: |
Description
The action hook wp_insert_site
will not allow wp_insert_post() to be ran.
wp_insert_site() will return Could not insert post into the database: Table "wordpress.wp_9_posts" doesn't exist
The old action 'wpmu_new_blog' works with wp_insert_post() in it.
Working with old action hook wpmu_new_blog
<?php add_action( 'wpmu_new_blog', function ( $blog_id ) { switch_to_blog( $blog_id ); $home_page_id = wp_insert_post( array( 'post_title' => 'Home', 'post_name' => 'home', 'post_content' => '', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'page', 'page_template' => 'page-templates/location_home.php', ) , true); echo 'dump:'; var_dump($home_page_id); restore_current_blog(); } );
This fails, using new action hook 'wp_insert_site'.
<?php add_action( 'wp_insert_site', function ( $wp_site ) { switch_to_blog( $wp_site->blog_id ); $home_page_id = wp_insert_post( array( 'post_title' => 'Home', 'post_name' => 'home', 'post_content' => '', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'post' ) , true); echo 'dump:'; var_dump($home_page_id); restore_current_blog(); } );
Change History (8)
#3
@
5 years ago
- Component changed from General to Networks and Sites
- Focuses multisite added
- Keywords reporter-feedback added
Hi @bikecrazyy, thanks for opening a ticket.
The site's database tables are added in wp_initialize_site()
, which is attached to the wp_initialize_site
hook at the default priority and run after the site is inserted into the wp_blogs
table in wp_insert_site()
.
I haven't tested this myself, but you should be able to hook into wp_initialize_site
at a priority higher than 10 and the database tables will be available.
#4
follow-up:
↓ 5
@
5 years ago
@jeremyfelt
Thanks for responding, I changed add_action priority to 5, 10, 15, and 999 but still same issue.
#5
in reply to:
↑ 4
@
5 years ago
Replying to bikecrazyy:
I changed add_action priority to 5, 10, 15, and 999 but still the same issue.
Did you change the action to wp_initialize_site
from wp_insert_site
?
Also, to clarify: the priority number should be more than 10, which I guess is a "lower" priority.
add_action( 'wp_initialize_site', function( $site_id ) { // do things }, 11, 1 );
Note, the tables do make it to the database once the query is complete but not untill then. If you call var_dump( $wpdb->tables() ); before the insert you will see it show the table names. But when wp_insert_post is called directly after it will show the error above.