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.

This is the function called with ajax:

Read More
<?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. 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. Here the wp_update_post description.

The problem is that this page are not really changed. What is wrong?

Related posts

1 comment

  1. 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)

Comments are closed.