admin管理员组文章数量:1122846
I want to run a custom function whenever posts are imported to my custom post type.
I am currently running this code -
function on_post_import($post){
global $wpdb;
$results = $wpdb->get_results(
"SELECT ID FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'"
);
foreach ($results as $result) {
// run custom function
}
$wpdb->flush();
}
add_action( 'wp_insert_post', 'on_post_import' );
The problem I am facing is that it does not work for last imported post. So if there are 3 posts imported then it works fine for 2 of those posts but not for the 3rd one (last imported).
UPDATE: If there is only one post imported the custom function does not work.
UPDATE2: Seems like the issue is with my custom function which makes use of get_post_meta() which does not seem to output anything.
UPDATE 3: I am now simply using the wpdb query to run custom function. I want to avoid it as it will keep running in the background. If anyone have a better solution then do let me know thanks!
global $wpdb;
$results = $wpdb->get_results(
"SELECT ID FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'"
);
foreach ($results as $result) {
// run custom function
}
$wpdb->flush();
What wrong am I doing? Or is there any other way to get my function to work on all imported posts including the last imported?
I want to run a custom function whenever posts are imported to my custom post type.
I am currently running this code -
function on_post_import($post){
global $wpdb;
$results = $wpdb->get_results(
"SELECT ID FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'"
);
foreach ($results as $result) {
// run custom function
}
$wpdb->flush();
}
add_action( 'wp_insert_post', 'on_post_import' );
The problem I am facing is that it does not work for last imported post. So if there are 3 posts imported then it works fine for 2 of those posts but not for the 3rd one (last imported).
UPDATE: If there is only one post imported the custom function does not work.
UPDATE2: Seems like the issue is with my custom function which makes use of get_post_meta() which does not seem to output anything.
UPDATE 3: I am now simply using the wpdb query to run custom function. I want to avoid it as it will keep running in the background. If anyone have a better solution then do let me know thanks!
global $wpdb;
$results = $wpdb->get_results(
"SELECT ID FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'"
);
foreach ($results as $result) {
// run custom function
}
$wpdb->flush();
What wrong am I doing? Or is there any other way to get my function to work on all imported posts including the last imported?
Share Improve this question edited Jul 7, 2014 at 7:58 Addicted asked Jul 6, 2014 at 14:33 AddictedAddicted 11 silver badge2 bronze badges 5- 2 There is nothing in this code that would be impacted by order of posts imported. Given same code works on all of them it should run for all of them... – Rarst Commented Jul 6, 2014 at 14:51
- Yes, even I am puzzled why won't it work for the last imported post or if there is only one post imported. :( – Addicted Commented Jul 6, 2014 at 15:15
- @Rarst could you recommend any other way to run my custom function once all posts are imported? – Addicted Commented Jul 6, 2014 at 15:20
- Any PHP errors? – birgire Commented Jul 6, 2014 at 15:59
- unfortunately, no errors :( – Addicted Commented Jul 6, 2014 at 16:03
1 Answer
Reset to default 0The 'wp_insert_post' action is fired for each post that is inserted and passed the ID of the inserted post to the callback function, so I think your $wpdb query is unnecessary - in fact, I think you're running the "custom function" portion for all published posts, not just the inserted post (which may or may not lead to issues depending on what you're actually doing).
function on_post_import( $post_id ) {
// custom function using $post_id
}
add_action( 'wp_insert_post', 'on_post_import' );
本文标签: pluginsAction hook quotwpinsertpostquot works but not for last imported post
版权声明:本文标题:plugins - Action hook "wp_insert_post" works but not for last imported post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736294012a1929265.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论