Suppose there is an array of tags:

array('name1'=>'metka1', 'name2'=>'metka2', 'name3'=>'metka1')... 

the key is the name of the record, and the value is respectively the label that you want to assign to this record by name.
Those. you must first create a programmatically new labels from the array, then assign these labels to the records that correspond to these labels in the array.
How to implement it?

  • what? what do you want to do specifically? - Ruslan
  • @Ruslan, I want to create tags and assign existing tags to existing entries by key, i.e. the key is the name of the record, and the value is the label that you want to assign to this record. Rubrics do not need to be assigned to entries prntscr.com/ezgc02 - word

1 answer 1

The code for solving the problem is:

 $tags_arr = array('name1'=>'metka1', 'name2'=>'metka2', 'name3'=>'metka1'); foreach ($tags_arr as $key=>$value) { $post = get_page_by_title( $key, OBJECT, array('post') ); // найти запись по названию if ($post) { $append = true; // true означает добавить метки к существующим wp_set_post_tags($post->ID, $value, $append); // добавить метки к посту } }