Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
1  
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 Jul 6 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. :( –  Gautam Jul 6 at 15:15
    
@Rarst could you recommend any other way to run my custom function once all posts are imported? –  Gautam Jul 6 at 15:20
    
Any PHP errors? –  birgire Jul 6 at 15:59
    
unfortunately, no errors :( –  Gautam Jul 6 at 16:03

1 Answer 1

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' );
share|improve this answer
    
Thanks for taking out the time to post. Unfortunately this is not working at all and I think it is all because of my custom function which makes use of post meta to display information on one of the custom columns I have. I make use of get_post_meta() to store information in a variable and then outputting as required. –  Gautam Jul 7 at 5:51
    
Seems like get_post_meta() is unavailable. –  Gautam Jul 7 at 5:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.