简体   繁体   English

在WordPress的save_post钩子之外更新页面数据

[英]update page data outside the save_post hook in wordpress

I'm trying to work on a bulk data update in a lot of page. 我正在尝试在很多页面上进行批量数据更新。 The ajax operation start with a click on button on admin page. ajax操作首先在管理页面上单击按钮。

This is the function called with ajax: 这是用ajax调用的函数:

<?php
function bp_update(){
    global $wpdb;

    $ret=Array();

    check_ajax_referer( 'bp_update', 'nonce');

    $new_title=$_POST['title'];
    $cats=$_POST['cats'];

    $toupdate=0;
    $updated=0;
    $failed=0;
    foreach ($cats as $cat){
        $post_ids=get_objects_in_term( $cat, 'attachment_category');
        if($new_title == ""){
            $term=get_term($cat, 'attachment_category');
            $new_title=$term->name;
        }
        $ret[$cat]=Array('pid' => $post_ids, 'title' => $new_title, 'page' => Array());
        foreach($post_ids as $id){
            $toupdate++;
            $pdf=get_post($id);

            $pdf_title=explode("-", $pdf->post_title);
            $name=ucwords(strtolower(str_replace('_',' ',$pdf_title[0])));
            $page=get_page_by_title($name);

            $new_content='[vc_row type="in_container" bg_position="left top" bg_repeat="no-repeat" scene_position="center" text_color="dark" text_align="left" top_padding="60"]'.
                            '[vc_column width="1/1"]'.
                                '[vc_column_text]'.
                                    '<h3>Fix text: <a href="'.$pdf->guid.'">'.$new_title.'</a></h3>'.
                                '[/vc_column_text]'.
                                '[divider line_type="Full Width Line" custom_height="10"]'.
                            '[/vc_column]'.
                        '[/vc_row]';

            $new_page = array(
                'ID'           => $page->ID,
                'post_content'   => $new_conten."\n".$page->post_content,
                'post_modified' => date("Y-m-d H:i:s"),
                'post_modified_gmt' => date("Y-m-d H:i:s"),
            );

            $err = wp_update_post( $new_page, true);

            array_push($ret[$cat]['page'], Array(
                'id'=> $new_page['ID'], 
                'mod' => $new_page['post_modified'],
                'err' => $err,
            ));
            /*$new_page['err'] = $err;
            array_push($ret[$cat]['page'], $new_page);*/

            if (is_wp_error($post_id)) {
                $failed++;

                $errors = $post_id->get_error_messages();
                /*foreach ($errors as $error) {
                    echo $error;
                }*/
            } else {
                $updated++;
            }

        }

    }

    wp_send_json_success($ret);
    die();
}
?>

This is what ajax return. 这就是ajax返回的内容。 It's correct: 这是正确的:

{ 
    "success":true,
    "data":{
        "11":{
            "pid":["1571","1572"],
            "title":"Luglio 2015",
            "page":[
                {
                    "id":1566,
                    "mod":"2015-05-25 10:32:05",
                    "err":1566
                },
                {   
                    "id":1569,
                    "mod":"2015-05-25 10:32:05",
                    "err":1569
                }
            ]
        }
    }
}

"id" is the same of "err" in every page and this mean that the update is gone well. 每个页面中的“ id”与“ err”相同,这意味着更新进展顺利。 Here the wp_update_post description. 这里是wp_update_post的描述。

The problem is that this page are not really changed. 问题是此页面没有真正更改。 What is wrong? 怎么了?

You've got a typo 你有错字

        $new_page = array(
            'ID'           => $page->ID,
            'post_content'   => $new_conten."\n".$page->post_content,
            'post_modified' => date("Y-m-d H:i:s"),
            'post_modified_gmt' => date("Y-m-d H:i:s"),
        );

should be 应该

        $new_page = array(
            'ID'           => $page->ID,
            'post_content'   => $new_content."\n".$page->post_content,
            'post_modified' => date("Y-m-d H:i:s"),
            'post_modified_gmt' => date("Y-m-d H:i:s"),
        );

($new_content instead of $new_conten) (用$ new_content代替$ new_conten)

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

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