Dynamically creating custom post type items and updating them

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?

Read More

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.

Related posts

Leave a Reply

2 comments

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

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