Reputation: 910
I have a created a custom post type Properties and within that I have created a custom Taxonomy called Position and within that I want to create some terms I have created the term Slider using the code below and that works fine.
function realestatepro_custom_terms() {
wp_insert_term(
'Slider', // the term
'Position', // the taxonomy
array(
'description'=> 'Will be featured in the home page slider.',
'slug' => 'home-page-slider'
)
);
}
add_action( 'init', 'realestatepro_custom_terms' );
But I want to create more terms, like Featured and Promoted, but I am not sure how, I did consider just repeating the whole wp_insert_terms block but that doesn't seem right, then I tried to just add another term straight after the first one but that just didn't work.
Anyone know the best way to add multiple terms?
Upvotes: 0
Views: 2348
Reputation: 3899
wp_insert_term()
is a big function with a lot of cleaning, actions and error checking. There isn't a version of it that repeats. So you'll have to wrap wp_insert_term()
into a foreach
loop.
function realestatepro_custom_terms() {
$terms = array(
'Slider' => array(
'description'=> 'Will be featured in the home page slider.',
'slug' => 'home-page-slider'
),
'Featured' => array( /*properties*/
),
'Promoted' => array( /*properties*/
)
);
foreach($terms as $term => $meta){
wp_insert_term(
$term, // the term
'Position', // the taxonomy
$meta
);
}
}
add_action( 'init', 'realestatepro_custom_terms' );
Another possibility is you could use wp_set_object_terms()
like this:
$terms = array( 'Featured', 'Promoted', 'Slider');
wp_set_object_terms( $object_id, $terms, 'Position' );
where $object_id
is a dummy Properties post you create. After adding the terms you could delete the post. The problem here is that you can't set any of the term meta like the slug or description. Furthermore the wp_set_object_terms()
function simply contains a foreach
loop with wp_insert_term()
repeated similar to the first solution. It's nothing too spectacular. I don't really recommend this second option, just mentioning for interest.
Upvotes: 3