wordpress: "wp_insert_post()" - break page
when i add function "wp_insert_post()" - break page. In database "$pages" array insert many times the same data... stop only when break pages. Why?
Thanks ;]
add_action('save_post', 'save_data_all');
function save_data_all($post_id)
{
$pages = array(
'post_title' => 'title',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_type' => 'page'
);
wp_insert_post($pages);
if(get_post_meta($post_id, 'l_news', true))
update_post_meta($post_id, 'l_news', $ser);
else
add_post_meta($post_id, 'l_news', $ser, false);开发者_如何学Python
}
If you get a blank page with a PHP application, it means that a most likely fatal error occurred. Sometimes it's also when an exception is thrown which is not in a try {} catch () {}
-block.
In your case, you need to figure out where your PHP error_log
is located. The easiest way to find out is to create a .php
page with the following code: <?php phpinfo();
.
If the directive is empty, set it. It's something you do in your php.ini
. Sometimes webhosters also provide you with an interface to do these settings. But you didn't share that much information.
So anyway, find the error_log
setting and then open the log file and investigate the error which occurred. It should be at or near the end of the file.
If you need more help debugging the error, share the error message and leave a comment.
The update_post_meta doesn't know the $post_id yet until AFTER the post is saved. In the conditional it will always return false.
You need to define the new $post_id when your inserting the post. Try this:
add_action('save_post', 'save_data_all');
function save_data_all()
{
$pages = array(
'post_title' => 'title',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_type' => 'page'
);
$post_id =wp_insert_post($pages);
if(get_post_meta($post_id, 'l_news', true))
update_post_meta($post_id, 'l_news', $ser);
else
add_post_meta($post_id, 'l_news', $ser, false);
}
""$pages" array insert many times the same data... stop only when break pages. Why?"
This is causing recursion, as the 'save_post' hook is fired in the wp_insert_post() function..so yeah it will happen over and over again!
also gets called in wp_update_post as well (wp_update_post actually uses wp_insert_post internally) ...
精彩评论