• I have a post and i need to update its post_parent. So i have used below code to update the post_parent value. Old post_parent is 844 and using query i have updated the value to 370 using below:

        global $wpdb;
        $table_name = $wpdb->prefix . "posts";
        $add_plan_to_team = $wpdb->update( $table_name, array( 'post_parent' => '370' ), array('ID'=> '84887'));
        // UPDATE wp_posts SET post_parent=370 WHERE ID=84887 

    In next line, i need to get the post again and then perform some functions. I am doing it like this
    $post = get_post( 84887 );

    But instead of returning updated values, i am getting the old post_parent value in return. Like below

    
    WP_Post Object
    (
        [ID] => 84887
        [post_author] => 2
        [post_date] => 2020-02-27 10:55:11
        [post_date_gmt] => 2020-02-27 10:55:11
        [post_content] => 
        [post_title] => MyTeam
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => closed
        [ping_status] => closed
        [post_password] => 
        [post_name] => allus-cu
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2020-08-24 19:03:02
        [post_modified_gmt] => 2020-08-24 19:03:02
        [post_content_filtered] => 
        [post_parent] => 844   // This value is not correct. This should be 370
        [guid] => https://train.localdev.com/?post_type=wc_memberships_team&p=84887
        [menu_order] => 0
        [post_type] => wc_memberships_team
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => db
    )
    

    I have checked in Database and in database, post_parent value is updated one.
    Complete Code in all together:

    global $wpdb;
    $table_name = $wpdb->prefix . "posts";
    $add_plan_to_team = $wpdb->update( $table_name, array( 'post_parent' => '370' ), array('ID'=> '84887'));
    
    if($add_plan_to_team){
        $post = get_post( $team_id, 'OBJECT', 'db' );
        echo '<h1> team_updated </h1>';
        print_r($post);            
    }
    

    Please tell me how can i fix it.

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get_post() is not returning correct value of post_parent’ is closed to new replies.