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
Add a comment  | 

1 Answer 1

Reset to default 0

The '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