0

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

3
  • Judging from your comment below, this looks like an XY problem, where you needed to update posts inside the save_posts hook but ran into recursion, so you devised a solution of using WPDB directly and it didn't work. Then, rather than ask how to avoid recursion in save_post, you instead asked how to fix your broken fix. Is this correct? There is a canonical solution for your problem, but I can't post it because that's not what you asked
    – Tom J Nowell
    Commented Aug 25, 2020 at 8:35
  • Yes thats correct. To avoid recursion in save post, i have used wpdb query directly. You can share the correct way as well Commented Aug 25, 2020 at 10:39
  • Ah I see. I can't answer with that as it's not what you asked, it would mean the answer would make no sense to anybody looking. I recommend asking a new question specifically about your original problem, as it's not the same question
    – Tom J Nowell
    Commented Aug 25, 2020 at 11:13

1 Answer 1

1

That's not how you update posts. Because you used raw SQL to do it, none of the filters or actions fired, and none of WP_Cache was updated, so it'll return an old version of the post it fetched earlier when you call get_post.

Instead, if you want to update a post, use wp_insert_post and pass the posts ID parameter.

E.g.

$result = wp_insert_post( [
    'ID' => 84887,
    'post_parent' => 370,
] );

Additional notes:

  • Don't use raw SQL to do things if there's an API ( for this there were several )
  • Don't hardcode post IDs, use slugs if you must hardcode anything. All it takes is an accidental post deletion and the code has to be changed at least with slugs you can rename a post and it works again ( and it won't work if you migrate the site with hardcoded numbers )
  • numbers aren't strings, use 5 not '5' especially when using SQL functions
4
  • Hello, I cannot use wp_insert_post() or wp_update_post() because i am inside save_post_{$post->post_type} hook and wp_update_post() re-triggers the save_pos_{$post->post_type} hook and instead of moving to next line, pointer moves to the very first line in the same function. I need to update value in DB without triggering save_post_{$post->post_type} Commented Aug 25, 2020 at 8:07
  • Secondly hard code values mentioned about are just for reference. I am inside a loop and all values are dynamic there. No hard coded values are used. Commented Aug 25, 2020 at 8:08
  • That's not true, you can with the right safeguards, and it was a super important piece of info you left out. Can you provide more context? It sounds like you tried to make the question generic/abstract but went too far. Remember, i'm not giving you a solution to your problem, I'm giving you an answer to your question, your question made no mention of save post, so update it with the relevant info
    – Tom J Nowell
    Commented Aug 25, 2020 at 8:33
  • @OptimumCreative this may not be the solution to your problem, but it is the answer to your question. I'd recommend asking a new question for your original problem
    – Tom J Nowell
    Commented Aug 25, 2020 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.