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).

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?

Read more here: Action hook "wp_insert_post" works but not for last imported post


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , , ,

Wordpress related questions and answers: