douji9816 2015-12-01 10:52
浏览 35
已采纳

Wordpress'add_post_meta' - 在手动打开帖子并“更新”后,数据不会显示在页面上

I'm using PHP automation to create new Wordpress posts (with wp_insert_post). I'm also using 'Simple Fields' to add additional meta fields for displaying on the front end.

My code creates the Wordpress post, keeps the post ID in a variable and then goes on to add the required meta data immediately afterwards.

Trimming all the other clutter, this is my process:

// Insert the post into the database and return the post ID
$post_id = wp_insert_post( $my_post );

// Add '$price' to relevant meta field
update_post_meta($post_id, '_simple_fields_fieldGroupID_1_fieldID_4_numInSet_0', $price);

This works fine, when I check the database, the 'price' meta field has the correct information inserted.

The problems starts when I try to extract it using:

echo simple_fields_value("price", $post->ID);

The code works - But the only way I can get it to pull the data is to log into the Wordpress admin, open the post for editing and then hit 'Update'.

Providing I open every post and hit 'Update', there's not a problem with my code... it does everything as expected.

My question is, what is the 'Update' button doing that I'm not? I've tried using 'wp_update_post' to re-open the post and save after the meta data is added to see if that helped, but it doesn't.

// Open and update post to ensure meta data saves correctly
$update_post = array(
  'ID'           => $post_id,
  'post_title'   => $title
);

// Update the post into the database
wp_update_post($update_post);

Your help on this would be much appreciated,

  • 写回答

1条回答 默认 最新

  • douyuepi6485 2015-12-01 11:21
    关注

    It looks like you are using the Simple Fields Plugin. I have never personally used this plugin, however I had a quick look and it looks like the plugin runs a few extra things on the save of the post. Here is one of the queries it uses for some sort of cacheing :

    // Save info about the fact that this post have been saved. This info is used to determine if a post should get default values or not.
            update_post_meta($post_id, "_simple_fields_been_saved", "1");
    

    Since you are inserting this meta value manually, I would recommend you use the native wordpress get_post_meta function to call the value, your query should look something like this:

    get_post_meta($post->ID, "_simple_fields_fieldGroupID_1_fieldID_4_numInSet_0");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line