I am creating a plugin which uses a custom post type. My question is two folds:
(1) upon activation of my plugin how do I create the items of my custom post types. For example: if my post type was say… “Best Restaurants”. I want to create 10 custom post types items since my plugin will need this information. How would I do that? Is there a function I can use which adds a post item along with its custom data?
and (2) When my plugin is updated, say I released a new version, what is the best way to modify this list? I was thinking of deleting all items with a certain post type and then inserting the new ones, but that might be overkill.
Thanks in advance.
Yes @wyrfel is right, you use
wp_insert_post()
to create your posts. Using your 50 US States example I’ve created some code you can drop into your theme’sfunctions.php
to see how it works (although you’ll probably not want to calladd_states_if_not_yet_added()
for every page load, but the example is easier to show it this way):And here’s some screenshots showing it in use:
For (1): use
wp_insert_post()
.For (2): One idea would be to insert the posts as above and store the insert ids in an array. Then save that array as an option. When updating, read the option and do ‘wp_update_post()’ on these ten posts that you now have the ID of.