I see a problem in the register_taxonomy
function. The second argument that you are using is:
array ( 0 => 'frontiles' )
But it should be:
array ( 'frontiles' )
Or, as you are passing only one CPT, it could be also a string:
'frontiles'
Also, you are executing insert_term()
in every page load but in your code I see that always is the same term inserted. Once the term was created the first time, there is no reason to repeat that process in every page load. I suggest to remove add_action('init','insert_term');
and execute insert_term()
on the registration action hook right before wp_insert_post
.
Also, because you want to insert a term and create posts during activation, you have to register the custom post type and taxonomy also in the register_activation_hook
, not only at init
. That is beacuse the next init
event ocurrs when the plugin is still inactive. I’ve build this plugin with your code and my suggestions and it works:
<?php
/*
Plugin Name: Test
Plugin URI: http://wordpress.stackexchange.com/questions/172888/plugin-development-hooks-generate-content
Version: 1.0
Author: CybMeta
Description: Test
*/
// Custom Post Type Front tiles
add_action('init', 'cptui_register_my_cpt_frontiles');
function cptui_register_my_cpt_frontiles() {
register_post_type('frontiles', array(
'label' => 'Tiles',
'description' => 'Create tiled elements for the front page.',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'tiles', 'with_front' => true),
'query_var' => true,
'exclude_from_search' => true,
'menu_position' => 3,
'supports' => array('title','editor','excerpt','thumbnail'),
'taxonomies' => array('tiles_categories'),
'labels' => array (
'name' => 'Tiles',
'singular_name' => 'Tile',
'menu_name' => 'Tiles Posts',
'add_new' => 'Add Tile',
'add_new_item' => 'Add New Tile',
'edit' => 'Edit',
'edit_item' => 'Edit Tile',
'new_item' => 'New Tile',
'view' => 'View Tile',
'view_item' => 'View Tile',
'search_items' => 'Search Tiles',
'not_found' => 'No Front Tiles Found',
'not_found_in_trash' => 'No Front Tiles Found in Trash',
'parent' => 'Parent Tile',
)
) );
}
// Taxonomies Front tiles
add_action('init', 'cptui_register_my_taxes_tiles_categories');
function cptui_register_my_taxes_tiles_categories() {
register_taxonomy( 'tiles_categories',array (
'frontiles'
),
array( 'hierarchical' => true,
'label' => 'Tile Categories',
'show_ui' => true,
'query_var' => true,
'show_admin_column' => true,
'labels' => array (
'search_items' => 'Search Tile Categories',
'popular_items' => '',
'all_items' => 'All Categories',
'parent_item' => 'Parent Tile Category',
'parent_item_colon' => 'Parent Tile Category',
'edit_item' => 'Edit Tile Category',
'update_item' => 'Update Tile Category',
'add_new_item' => 'Add Tile Category',
'new_item_name' => 'New Tile Category',
'separate_items_with_commas' => 'Separate tile categories with commas',
'add_or_remove_items' => 'Add Categories...',
'choose_from_most_used' => 'Choose from the most used Tiles Categories',
)
) );
}
////////////// Add term "mosaic-home" to custom taxonomy "tiles_categories"
function insert_term() {
wp_insert_term(
'Mosaic - Home',
'tiles_categories',
array(
'description' => 'Add Tiles here to load in first term'
)
);
}
function create_frontles_posts() {
cptui_register_my_cpt_frontiles();
cptui_register_my_taxes_tiles_categories();
insert_term();
$x = 1;
do {
$post_id = wp_insert_post(array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => 1,
'post_name' => 'tile'.$x,
'post_title' => 'Tile',
'post_status' => 'publish',
'post_type' => 'frontiles',
));
$term_taxonomy_ids = wp_set_object_terms( $post_id, array('mosaic-home'), 'tiles_categories' );
$x++;
} while ($x <= 24);
}
register_activation_hook( __FILE__, 'create_frontles_posts' );