WP插入PHP函数和自定义字段
4 个回答
- 投票数
-
- 2011-02-04
如果您阅读了
wp_insert_post
的文档,它将返回您刚刚创建的帖子的帖子ID.如果将其与以下功能
__update_post_meta
结合使用(我从此站点获得并修改了一个自定义功能)/** * Updates post meta for a post. It also automatically deletes or adds the value to field_name if specified * * @access protected * @param integer The post ID for the post we're updating * @param string The field we're updating/adding/deleting * @param string [Optional] The value to update/add for field_name. If left blank, data will be deleted. * @return void */ public function __update_post_meta( $post_id, $field_name, $value = '' ) { if ( empty( $value ) OR ! $value ) { delete_post_meta( $post_id, $field_name ); } elseif ( ! get_post_meta( $post_id, $field_name ) ) { add_post_meta( $post_id, $field_name, $value ); } else { update_post_meta( $post_id, $field_name, $value ); } }
您将获得以下信息:
$my_post = array( 'post_title' => $_SESSION['booking-form-title'], 'post_date' => $_SESSION['cal_startdate'], 'post_content' => 'This is my post.', 'post_status' => 'publish', 'post_type' => 'booking', ); $the_post_id = wp_insert_post( $my_post ); __update_post_meta( $the_post_id, 'my-custom-field', 'my_custom_field_value' );
If you read the documentation for
wp_insert_post
, it returns the post ID of the post you just created.If you combine that with the following function
__update_post_meta
(a custom function I acquired from this site and adapted a bit)/** * Updates post meta for a post. It also automatically deletes or adds the value to field_name if specified * * @access protected * @param integer The post ID for the post we're updating * @param string The field we're updating/adding/deleting * @param string [Optional] The value to update/add for field_name. If left blank, data will be deleted. * @return void */ public function __update_post_meta( $post_id, $field_name, $value = '' ) { if ( empty( $value ) OR ! $value ) { delete_post_meta( $post_id, $field_name ); } elseif ( ! get_post_meta( $post_id, $field_name ) ) { add_post_meta( $post_id, $field_name, $value ); } else { update_post_meta( $post_id, $field_name, $value ); } }
You'll get the following:
$my_post = array( 'post_title' => $_SESSION['booking-form-title'], 'post_date' => $_SESSION['cal_startdate'], 'post_content' => 'This is my post.', 'post_status' => 'publish', 'post_type' => 'booking', ); $the_post_id = wp_insert_post( $my_post ); __update_post_meta( $the_post_id, 'my-custom-field', 'my_custom_field_value' );
-
非常感谢.你能给我一个关于植入的想法吗?IE浏览器代码中的哪一个去哪里.非常感谢Thanks very much. Could you possibly give me an idea on the implantation. IE. which but of code goes where. Many Thanks
- 0
- 2011-02-04
- Robin I Knight
-
做得很好.第二个代码块替换您的代码块,函数值是自定义字段键/值对.将函数放在脚本顶部,或放在脚本顶部包含的单独的.php文件中.Nicely done. The second code block replaces yours, the function values is the custom field key/value pair. Put the function either at the top of the script, or in a separate .php file included at the top of the script.
- 2
- 2011-02-04
- aendrew
-
注意,我确实使用OOP,因此这就是在"功能"前面使用"public"修饰符的原因.如果您要包含函数本身而不将其放入类中,则无需添加`public`.As a note, I do use OOP so that's the reason for the `public` modifier in front of "function". If you're including the function itself without putting it into a class, you don't need to add `public`
- 1
- 2011-02-05
- Zack
-
你好扎克,阿恩德鲁和菲利普.一切工作正常,但是我尝试将其应用于查询也无济于事.我不太明白为什么.这是链接,因为您都知道初始自定义字段新帖子的工作方式,我认为您可能会看到我的错误.http://wordpress.stackexchange.com/questions/8622/wp-insert-post-php-function-dynamically-generated-custom-fieldsHello Zack, Aendrew and Philip. Everything is working beautifully however I tried to apply it to a query as well to no avail. I don't quite see why. Here is the link since you all know how the initial custom field new post worked I thought you might see my error. http://wordpress.stackexchange.com/questions/8622/wp-insert-post-php-function-dynamically-generated-custom-fields
- 0
- 2011-02-05
- Robin I Knight
-
@Zack-好答案.但是我倾向于避免在函数名称中使用前导下划线,仅是因为平台开发人员在想要创建_" reserved" _或_(pseudo)" hidden" _函数时容易被平台开发人员使用,所以人们看到那些人可能认为它们是平台功能,或者更糟的是,它们可能与将来的WordPress核心功能发生冲突.只是一个想法.@Zack - Nice answer. But I would tend to shy away from using leading underscores in function names if only because names with leading underscores tend to be used by platform developers when they want to create _"reserved"_ or _(pseudo) "hidden"_ functions so people seeing those might think they are platform functions, or worse they might conflict with a future WordPress core function. Just a thought.
- 0
- 2011-02-05
- MikeSchinkel
-
@MikeSchinkel:最初,我最初开始对类中的受保护函数使用" __",但后来将插件的结构更改为该函数需要公开的位置.我也将其简单地命名为" update_post_meta",但这肯定与WordPress的核心功能冲突.所以我不知道该怎么称呼:(@MikeSchinkel: At first, I originally started using "__" for protected functions within a class, but then changed the structure of my plugin to where the function needed to be public. I also would've named it simply "update_post_meta" but that would've definitely conflicted with WordPress' core functions. So I didn't know what to name it :(
- 0
- 2011-02-05
- Zack
-
@Zack-也许是`zacks_update_post_meta()`?:)我没有在开头的下划线提到的另一个问题是,其他同样富有创意的人可能在插件中使用相同的约定,因此与您的约定冲突.顺便说一句,您是否知道`update_post_meta()`是否会添加?仅当您要添加重复的键时才需要`add_post_meta()`.@Zack - Maybe `zacks_update_post_meta()`? :) The other issue I didn't mention with leading underscores is that someone else equally creative may use the same convention in a plugin and thus conflict with yours. BTW, did you know that `update_post_meta()` adds if it doesn't exist? `add_post_meta()` is only needed when you want to add duplicate keys.
- 0
- 2011-02-05
- MikeSchinkel
-
@MikeSchinkel:我最初在此站点的其他地方找到了该函数,喜欢它,并仅仅因为我喜欢就对其进行了重构.我也认为`update_post_meta()`已经做到了,但是不能确定.找到功能后,我便得出结论.@MikeSchinkel: I originally found the function elsewhere on this site, liked it, and refactored it just because I'm like that. I also figured `update_post_meta()` did that already, but couldn't be sure. When I found the function, I jumped to conclusions.
- 0
- 2011-02-05
- Zack
-
@Zack-嘿学习WordPress是一个旅程.我认为我还剩下至少50%的旅程,如果不是更多的话!@Zack - Hey learning WordPress is a journey. I figure I still have at least 50% more of that journey left, if not a lot more!
- 0
- 2011-02-05
- MikeSchinkel
-
我无法添加答案,因为我在wordpress.stackexchange上没有声誉.从今天起,有了一个新方法,您可以简单地将数组放入wp_insert_post中,如下所示:meta_input=> array(key=> value)I can't add an answer, as I don't have reputation on wordpress.stackexchange. As of today there is a new method, you can simply put in an array into wp_insert_post as: meta_input => array(key=>value)
- 1
- 2016-10-15
- Frederik Witte
-
- 2011-02-05
您可以在" wp_insert_post"之后简单地添加" add_post_meta"
<?php $my_post = array( 'post_title' => $_SESSION['booking-form-title'], 'post_date' => $_SESSION['cal_startdate'], 'post_content' => 'This is my post.', 'post_status' => 'publish', 'post_type' => 'booking', ); $post_id = wp_insert_post($my_post); add_post_meta($post_id, 'META-KEY-1', 'META_VALUE-1', true); add_post_meta($post_id, 'META-KEY-2', 'META_VALUE-2', true); ?>
You can simple add the 'add_post_meta' after the 'wp_insert_post'
<?php $my_post = array( 'post_title' => $_SESSION['booking-form-title'], 'post_date' => $_SESSION['cal_startdate'], 'post_content' => 'This is my post.', 'post_status' => 'publish', 'post_type' => 'booking', ); $post_id = wp_insert_post($my_post); add_post_meta($post_id, 'META-KEY-1', 'META_VALUE-1', true); add_post_meta($post_id, 'META-KEY-2', 'META_VALUE-2', true); ?>
-
- 2011-02-04
使用
save_post
过滤器,然后在过滤器函数中调用add_post_meta
.Use
save_post
filter, then calladd_post_meta
in your filter function.-
没用$post-> ID对wp_insert_post_data不可用,这是创建自定义字段所必需的.Unhelpful. $post->ID is not available to wp_insert_post_data, which is necessary for creating custom fields.
- 0
- 2011-02-04
- aendrew
-
@aendrew`save_post`动作在函数的最后,它传递了帖子的ID和对象,答案是正确的.@aendrew `save_post` action is at the very end of the function, it has post's ID and object passed to it, answer is sound.
- 0
- 2011-02-05
- Rarst
-
我很确定这是经过编辑的,拉斯特.无论如何,这现在很有意义.I'm pretty sure this was edited, Rarst. Regardless, it makes sense now.
- 1
- 2011-02-05
- aendrew
-
@aendrew啊,对不起-没注意到@aendrew ah, sorry - didn't notice that
- 0
- 2011-02-05
- Rarst
-
- 2011-02-04
我认为您不能将其与wp_insert_post();一起使用.
原因是由于WP如何存储两种数据类型.帖子存储在一个大的整体表中,该表具有十几个不同的列(wp_posts);自定义字段存储在一个简单的4列表(wp_postmeta)中,该表主要由与帖子关联的元键和值组成.
因此,只有拥有帖子ID才能真正存储自定义字段.
尝试一下:
function myplugin_insert_customs($pid){ $customs = array( 'post_id' => $pid, 'meta_key' => 'Your meta key', 'meta_value' => 'Your meta value', ); add_post_meta($customs); } add_action('save_post', 'myplugin_insert_customs', 99);
此法典帖子提供了帮助-与您正在做的事情相反(即在帖子删除后删除数据库行): http://codex.wordpress.org/Plugin_API/Action_Reference/delete_post
I don't think you can use it with wp_insert_post();.
The reason is because of how WP stores the two data types. Posts are stored in one big monolithic table with a dozen different columns (wp_posts); custom fields are stored in a simpler, 4-column table (wp_postmeta) comprised mainly of a meta key and value, associated with a post.
Consequently, you can't really store custom fields until you have the post ID.
Try this:
function myplugin_insert_customs($pid){ $customs = array( 'post_id' => $pid, 'meta_key' => 'Your meta key', 'meta_value' => 'Your meta value', ); add_post_meta($customs); } add_action('save_post', 'myplugin_insert_customs', 99);
This codex post helped -- it's kinda the opposite of what you're doing (i.e., deleting a DB row upon post deletion): http://codex.wordpress.org/Plugin_API/Action_Reference/delete_post
-
在那种情况下,我看到的唯一出路是使用会话,这是正确的.In that case the only way out I can see is to use a session, would that be correct.
- 0
- 2011-02-04
- Robin I Knight
-
;我猜您的插件正在尝试在保存帖子的同时插入自定义字段,对吗?我认为您需要做的是在帖子保存后挂入WP,获取帖子的新ID号,然后将其提供给add_post_meta();.创建CF.我将在稍后用一些代码更新我的答案.Nah; I'm guessing your plugin is trying to insert custom fields at the same time a post is saved, right? I think what you need to do is hook into WP after the post is saved, grab the post's new ID number, then supply that to add_post_meta(); to create the CFs. I'll update my answer in a second with some code.
- 0
- 2011-02-04
- aendrew
-
谢谢您的帮助.顺便说一句,它不是一个插件.我写了它,所以我们可以根据需要进行自定义.(但不要以为这意味着我对php很好,只是反复试验)Thanks for the help. By the way its not a plugin. I wrote it so we can customise it as much as needed. (but don't take that to mean I'm any good with php, just trial and error)
- 0
- 2011-02-04
- Robin I Knight
-
那是一个主题吗?在这种情况下,只有真正的区别是将其放在functions.php中.It's a theme, then? Only real difference is you'd put that in functions.php, in that case.
- 0
- 2011-02-04
- aendrew
WordPress功能用于以编程方式提交数据.要提交的标准字段,包括内容,摘录,标题,日期等等.
没有文档说明什么是如何提交到自定义字段的.我知道可以使用
add_post_meta($post_id, $meta_key, $meta_value, $unique);
函数.但是,如何将其包含在标准的
wp_insert_post
函数中?