更新wordpress中save_post挂钩之外的页面数据

时间:2015-05-25 11:01:12

标签: php ajax wordpress

我正在尝试在很多页面中处理批量数据更新。 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();
}
?>

这就是ajax的回归。这是对的:

{ 
    "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”与每个页面中的“错误”相同,这意味着更新很顺利。 Here wp_update_post说明。

问题是这个页面并没有真正改变。有什么问题?

1 个答案:

答案 0 :(得分:0)

你有一个错字

        $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"),
        );

应该是

        $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而不是$ new_conten)