admin管理员组文章数量:1388805
I have two plugins, one uses the function wp_insert_post(), the other has a code like this:
add_action('future_to_publish', 'myFunc', 10, 1);
add_action('new_to_publish', 'myFunc', 10, 1);
add_action('draft_to_publish', 'myFunc', 10, 1);
function myFunc( $postID ) {
}
When the first plugin runs wp_insert_post(), $postID is always empty. If I use the hook "publish_post" and press update then $postID does have a value, so what I am doing wrong?
I have two plugins, one uses the function wp_insert_post(), the other has a code like this:
add_action('future_to_publish', 'myFunc', 10, 1);
add_action('new_to_publish', 'myFunc', 10, 1);
add_action('draft_to_publish', 'myFunc', 10, 1);
function myFunc( $postID ) {
}
When the first plugin runs wp_insert_post(), $postID is always empty. If I use the hook "publish_post" and press update then $postID does have a value, so what I am doing wrong?
Share Improve this question asked Apr 29, 2012 at 1:44 pliomnpliomn 11 silver badge1 bronze badge3 Answers
Reset to default 5- Read the Codex,
- Look at the function in the core file,
Modify your code as follows:
function myFunc( $post ) { $postID = $post->ID; }
The post transistion does not send the post ID, it sends the complete post object. Sometimes a simple die(var_dump($postID));
(or whatever you use as parameter) helps to find out what will be send to the callback. If you don't know how many parameters are send to the callback, put a die(var_dump(func_get_args()));
at the first line of your callback.
function myFunc( $post ) {
global $post;
$myPostId = $post->ID;
//your function here
}
For getting the $postID value, you just insert global $post
expression in your function.
It worked for me.
wp_insert_posts()
returns the post ID on success. The value 0 or WP_Error on failure.
$postId = wp_insert_post($args);
if(!$postId){
echo "Your post is not inserted that's why you've no post id :p"
}
本文标签: pluginsHow to get post ID with hooks publishpostnewtopublishetc
版权声明:本文标题:plugins - How to get post ID with hooks publish_post, new_to_publish, etc 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744577728a2613729.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论