SHOW:
|
|
- or go back to the newest paste.
| 1 | /** | |
| 2 | - | * Create slugs/terms in music_genre_posts taxonomy for all music_genres |
| 2 | + | * Create slugs/terms in jjm_author_tax taxonomy for all jjm_authors posts |
| 3 | * | |
| 4 | */ | |
| 5 | function make_taxonomy_from_posts($post_type, $taxonomy){
| |
| 6 | // Get all posts | |
| 7 | $query_posts = query_posts(array( | |
| 8 | // ... of the requested type | |
| 9 | 'post_type'=> $post_type, | |
| 10 | // ... and it supports the taxonomy | |
| 11 | 'taxonomy' => $taxonomy, | |
| 12 | // ... without limit | |
| 13 | 'nopaging' => true, | |
| 14 | )); | |
| 15 | ||
| 16 | // Reset the main query | |
| 17 | wp_reset_query(); | |
| 18 | ||
| 19 | foreach ($query_posts as $query_post) {
| |
| 20 | $post_id = $query_post->ID; | |
| 21 | $raw_title = $query_post->post_title; | |
| 22 | // Generate a slug based on the title. | |
| 23 | // We want to check for auto-draft so we don't create a term for a post with no title | |
| 24 | $slug_title = sanitize_title($raw_title); | |
| 25 | ||
| 26 | // Do the checks for empty titles | |
| 27 | // If the title is blank, skip it | |
| 28 | if ($slug_title == 'auto-draft' || empty($raw_title)) continue; | |
| 29 | // Get all of the terms associated with the post | |
| 30 | $terms = get_the_terms($post_id, $taxonomy); | |
| 31 | $term_id = 0; | |
| 32 | ||
| 33 | if (!empty($terms)) {
| |
| 34 | $term_id = $terms[0]->term_id; | |
| 35 | } | |
| 36 | ||
| 37 | if ($term_id > 0) {
| |
| 38 | // If the post has a term, update the term | |
| 39 | wp_update_term($term_id, $taxonomy, array( | |
| 40 | 'description' => $raw_title, | |
| 41 | 'slug' => $raw_title, | |
| 42 | 'name' => $raw_title, | |
| 43 | )); | |
| 44 | } else {
| |
| 45 | // Otherwise add a new term | |
| 46 | wp_set_object_terms($post_id, $raw_title, $taxonomy, false); | |
| 47 | } | |
| 48 | } | |
| 49 | } |