简体   繁体   English

wordpress:“wp_insert_post()”-中断页面

[英]wordpress: “wp_insert_post()” - break page

when i add function "wp_insert_post()" - break page.当我添加 function“wp_insert_post()”时 - 中断页面。 In database "$pages" array insert many times the same data... stop only when break pages.在数据库“$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); 

}

""$pages" array insert many times the same data... stop only when break pages. Why?" ""$pages" 数组多次插入相同的数据...仅在分页时停止。为什么?

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!这会导致递归,因为在 wp_insert_post() function 中触发了“save_post”钩子。所以是的,它会一遍又一遍地发生!

also gets called in wp_update_post as well (wp_update_post actually uses wp_insert_post internally)...也会在 wp_update_post 中被调用(wp_update_post 实际上在内部使用 wp_insert_post)...

If you get a blank page with a PHP application, it means that a most likely fatal error occurred.如果您得到一个包含 PHP 应用程序的空白页,这意味着很可能发生了致命错误。 Sometimes it's also when an exception is thrown which is not in a try {} catch () {} -block.有时也会抛出不在try {} catch () {} -block 中的异常。

In your case, you need to figure out where your PHP error_log is located.在您的情况下,您需要确定 PHP error_log的位置。 The easiest way to find out is to create a .php page with the following code: <?php phpinfo();最简单的查找方法是使用以下代码创建一个.php页面: <?php phpinfo(); . .

If the directive is empty , set it.如果指令为,则设置它。 It's something you do in your php.ini .这是您在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.所以无论如何,找到error_log设置,然后打开日志文件并调查发生的错误。 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. update_post_meta 直到帖子保存后才知道 $post_id。 In the conditional it will always return false.在有条件的情况下,它将始终返回 false。

You need to define the new $post_id when your inserting the post.您需要在插入帖子时定义新的 $post_id。 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); 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM