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

share|improve this question
    
"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" Can you elaborate on your example? I don't really understand what you mean by a "post types items"? Can you give a more complete example? –  MikeSchinkel Mar 2 '11 at 4:58
    
@MikeSchinkel I want to prepopulate the database with my custom post type. For example - If my plugin requires a list of states I would create a custom post type called "state" and prepopulate the database with 50 states. –  rexposadas Mar 2 '11 at 18:36
add comment

2 Answers

up vote 1 down vote accepted

Hi @rxn:

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's functions.php to see how it works (although you'll probably not want to call add_states_if_not_yet_added() for every page load, but the example is easier to show it this way):

<?php 

add_action('init','init_us_states');
function init_us_states() {
  register_us_states_post_type();
  add_states_if_not_yet_added();
}
function add_states_if_not_yet_added() {
  foreach(get_50_us_states() as $state_code => $state_name) {
    if (!get_page_by_path($state_code,OBJECT,'us-state'))
      wp_insert_post(array(
        'post_type'       => "us-state",
        'post_content'    => "Information about {$state_name}",
        'post_title'      => $state_name, // i.e. 'Georgia'
        'post_name'       => $state_code, // i.e. 'GA'; this is for the URL
        'post_status'     => "publish",
        'comment_status'  => "closed",
        'ping_status'     => "closed",
        'post_parent'     => "0",
      ));
  }
}
function register_us_states_post_type() {
  register_post_type('us-state',array(
      'labels' => array(
      'name' => _x('States', 'post type general name'),
      'singular_name' => _x('State', 'post type singular name'),
      'add_new' => _x('Add New', 'us-state'),
      'add_new_item' => __('Add New State'),
      'edit_item' => __('Edit State'),
      'new_item' => __('New State'),
      'view_item' => __('View State'),
      'search_items' => __('Search States'),
      'not_found' =>  __('No States found'),
      'not_found_in_trash' => __('No States found in Trash'),
      'parent_item_colon' => '',
      'menu_name' => 'States'
    ),
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'rewrite' => array('slug'=>'states'),
    'capability_type' => 'post',
    'has_archive' => 'states',
    'hierarchical' => false,
    'supports' => array('title','editor','author','thumbnail','excerpt')
  ));
}
function get_50_us_states() {
  return array(
    'AL' => 'Alabama',
    'AK' => 'Alaska',
    'AZ' => 'Arizona',
    'AR' => 'Arkansas',
    'CA' => 'California',
    'CO' => 'Colorado',
    'CT' => 'Connecticut',
    'DE' => 'Delaware',
    'FL' => 'Florida',
    'GA' => 'Georgia',
    'HI' => 'Hawaii',
    'ID' => 'Idaho',
    'IL' => 'Illinois',
    'IN' => 'Indiana',
    'IA' => 'Iowa',
    'KS' => 'Kansas',
    'KY' => 'Kentucky',
    'LA' => 'Louisiana',
    'ME' => 'Maine',
    'MD' => 'Maryland',
    'MA' => 'Massachusetts',
    'MI' => 'Michigan',
    'MN' => 'Minnesota',
    'MS' => 'Mississippi',
    'MO' => 'Missouri',
    'MT' => 'Montana',
    'NE' => 'Nebraska',
    'NV' => 'Nevada',
    'NH' => 'New Hampshire',
    'NJ' => 'New Jersey',
    'NM' => 'New Mexico',
    'NY' => 'New York',
    'NC' => 'North Carolina',
    'ND' => 'North Dakota',
    'OH' => 'Ohio',
    'OK' => 'Oklahoma',
    'OR' => 'Oregon',
    'PA' => 'Pennsylvania',
    'RI' => 'Rhode Island',
    'SC' => 'South Carolina',
    'SD' => 'South Dakota',
    'TN' => 'Tennessee',
    'TX' => 'Texas',
    'UT' => 'Utah',
    'VT' => 'Vermont',
    'VA' => 'Virginia',
    'WA' => 'Washington',
    'WV' => 'West Virginia',
    'WI' => 'Wisconsin',
    'WY' => 'Wyoming',
  );
}

And here's some screenshots showing it in use:

WordPress 3.1 post type page for a US State List of US States on a WordPress 3.1 archive page List of US State post types in the WordPress 3.1 admin

share|improve this answer
    
Thank you for the detailed answer. I noticed that when I use wp_insert_post() the user can see these posts under the "Post" tab. I don't want the user to see the states. It's only for the plugin. Is there a way to prevent it from showing up in the list of posts? –  rexposadas Mar 3 '11 at 20:23
    
@rxn - Remove 'publicly_queryable', 'show_ui', 'show_in_menu', 'query_var' and 'rewrite' from the call to register_post_type() and set 'public' => false. –  MikeSchinkel Mar 3 '11 at 21:37
    
Awesome. Thank you. –  rexposadas Mar 3 '11 at 22:26
    
@rxn - I see you are relatively new; you've asked 5 questions, "accepted" all the answers (good job!) but never voted up any of those answers. So here's a tip: Go back and vote up all those answers you accepted; it'll give a bit more 'reward" to those who've helped you and encourage more people to answer your questions. See wordpress.stackexchange.com/faq#howtoask for more info. –  MikeSchinkel Mar 3 '11 at 22:49
    
Thank you. I'll definitely keep that in mind. Previous to accepting your answer in this question I had less than 15 points. I am not able to vote up with less than 15 points. I know this because I tried to vote up several times. But now that I have more than 15 points and the tip you just gave me, I'll definitely make it a practice to vote for those who answers my questions. –  rexposadas Mar 4 '11 at 3:30
add comment

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.

share|improve this answer
add comment

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.