最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - get_post() is not returning correct value

programmeradmin0浏览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] => /?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
  • 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 – Sajid Manzoor 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
Add a comment  | 

1 Answer 1

Reset to default 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
发布评论

评论列表(0)

  1. 暂无评论