I'm creating a plugin that creates a post in a CPT for every Facebook page post (using fb graph api). When should I fire the function that gets the data? using an action? Also if I understood the docs correctly, the wp_insert_post takes ID as an argument, which if doesn't exist, sets the newly created post id to that value?


There's no way to get notifications from Facebook for a new post. You have to check by hand. Just set a transient with the desired expire time for the data so you don't make a request on each reload, maybe check hourly for new data or every 30 mins, up to you.

As for your second question, read the docs:


It automatically assigns an ID unless provided by you (which is auto-incremented by the system) but you still need to provide more things in the array, even if some are default, you might not want it like that.

| improve this answer | |
  • Thanks for replying. Good idea to set an expire time, coz for now I just set it to plugins_loaded action. About the ID, I miss-understood, just setting a value to ID doesnt set the ID (if not already set), apparently I can use 'import_id' instead.. still working on it coz it hasn't worked till now. – Silvester Vella May 14 '19 at 12:58
  • @SilvesterVella I mean, that's just how transients work. I'd honestly say not to touch the ID. Unless you have a very good reason for having a custom one (which, if you do, let me tell you, you're probably wrong), it messes up the internal auto-increment in the database, say you have a post with ID 352, but you suddenly decide to insert a post with ID 300, which doesn't yet exist, so it's valid, what will happen is that now your auto-increment is reset to 300 in the database, if I remember correctly.. – coolpasta May 14 '19 at 13:00
  • Thanks a lot for that info. To be honest, the idea was so that it won't create duplicate posts, so I figured if the ID exists (which was going to the date/time of the facebook post created) it will update that post, if not, create a new. Given what you said, better if I just check the post title or slug or someting. – Silvester Vella May 14 '19 at 13:15
  • @SilvesterVella Duplicates can never, ever happen in WordPress in terms of ID, but if you reset the auto-increment, your post "logic" might not make sense at some point, as in "why the hell does this have this ID but it was posted before this post with a higher ID?" – coolpasta May 14 '19 at 13:17
  • True. When I said duplicate posts, I meant because the plugin will create posts for every facebook post. So every time it gets the json data, it will create posts with the exact content. But if I just check the date or something before, shouldn't be a problem. I was complicating things for a minute there. – Silvester Vella May 14 '19 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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