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] => /?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.
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/?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.
Share Improve this question edited Aug 24, 2020 at 20:16 Sajid Manzoor asked Aug 24, 2020 at 19:16 Sajid ManzoorSajid Manzoor 1031 silver badge7 bronze badges 3 |1 Answer
Reset to default 1That'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
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 insave_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