wordpress update_post_meta和get_post_meta [英] wordpress update_post_meta and get_post_meta

查看:192
本文介绍了wordpress update_post_meta和get_post_meta的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为worpdress开发插件,但遇到了一个奇怪的问题.

I'm currently working on a plugin for worpdress and have ran into a strange problem.

我目前有一个前端表单,用户可以填写并提交.提交后,便会创建具有以下功能的自定义帖子类型的帖子:

I currently have a frontend form which the user fills in and submits. Once submitted this then creates a post in a custom post type, with the following function:

$my_post = array(
  'post_title'    => $title,
  'post_type'     => 'product_enquiries',
  'post_content'  => $message,
  'post_status'   => 'private',
  'post_author'   => 1,

);
$post_id = wp_insert_post( $my_post );


update_post_meta($post_id, '_user_email', $_POST["email"]);
update_post_meta($post_id, '_user_name', $_POST["name"]);
update_post_meta($post_id, '_user_phone', $_POST["phone"]);

wp_reset_postdata();

在同一页面上,我也有这个:

On the same page i also have this:

$pID = get_the_id(); 
$customemail = get_post_meta($pID, 'enquiry_email', true) ;

由于某种原因,提交表单然后创建帖子时,它将删除$ customemail的值.我已经一遍又一遍地经历了,无法解决?

For some reason when the form is submitted and then post is created, it deletes the value of $customemail. I have been through it over and over again and cant work it out?

我尝试将update_post_meta更改为add_post_meta,这没有什么区别,并且还添加了wp_reset_postdata();.再次没有区别.

I have tried changing update_post_meta to add_post_meta which made no difference, and have also added wp_reset_postdata(); which again makes no difference.

如果我删除插入新帖子的代码,则$ customemail保留其值.

If I remove the code that inserts the new post then $customemail retains its value.

有什么想法吗?

推荐答案

$post_id = wp_insert_post( $my_post );

仅在成功插入帖子后才会返回$post_id

this will only return a $post_id if post will be successfully inserted

您可能在$post_id = wp_insert_post( $my_post );

您应该首先检查带有'post_title' => $title的帖子是否已经退出,然后只有您应该插入帖子

you should first check if a post with 'post_title' => $title already exits if it not, then only you should insert post

这篇关于wordpress update_post_meta和get_post_meta的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆