0

I am adding few meta fields for product post type using wp_insert_postaction and it is working perfectly.

But I have to trigger my function upon product imports as well as this action is not firing on products import. Products are imported directly in database so Is there any way to trigger this function upon import. I will really appreciate your help.

1
  • 1
    To clarify, you want your function to fire when using the WordPress Importer or is there another method in-use for importing posts?
    – jdm2112
    Commented May 16, 2019 at 18:42

2 Answers 2

1

If you are trying to fire your function when the WordPress Importer runs, there are a few filters and action hooks available.

From the plugin documentation:

import_start: occurs after the export file has been uploaded and author import settings have been chosen

import_end: called after the last output from the importer

https://wordpress.org/plugins/wordpress-importer/

5
  • But the products are imported directly in database. So these above mention filters will not work in my case.
    – wplearner
    Commented May 16, 2019 at 18:53
  • How are the products being imported?
    – jdm2112
    Commented May 16, 2019 at 18:59
  • From Netsuite to directly in database of my website
    – wplearner
    Commented May 16, 2019 at 19:00
  • Then WordPress does not know about this activity at all. It is happening outside of WordPress execution. You will need to setup a cron job to cycle regularly or manually trigger the code after you complete an import. Perhaps you could create a MySQL trigger dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
    – jdm2112
    Commented May 16, 2019 at 19:06
  • Thank you for your suggestion. I will implement cron job for this.
    – wplearner
    Commented May 16, 2019 at 19:07
0

I checked and found none in WordPress Importer 0.8.1 fireing after all insertion of a posts have been made (terms, postmeta, comments)

Solution 1:

You can use the built in WordPress hook that fires after a post is inserted? However I'm not sure how to test if the post is an imported one or a normal one being written from dashboard.

add_action('wp_insert_post', function ($post_ID, $post, $update) {
    // do something with $post_ID
}, 10, 3);

Solution 2 not recommended:

You can hack in the WordPress importer plugin source code and add your custom action? I haven't tested so I'm not 100% sure about this. If they haven't add it maybe there is some reason. Add slightly above unset( $this->posts );, mind the closing brace } of the foreach $posts.

File: class-wp-import.php

    // custom hook... it will be lost at next plugin update.
    do_action( 'my_custom_wp_imported_post', $post_id, $post );
}

unset( $this->posts );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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