Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Create slugs/terms in jjm_author_tax taxonomy for all jjm_authors posts
- *
- */
- function make_taxonomy_from_posts($post_type, $taxonomy){
- // Get all posts
- $query_posts = query_posts(array(
- // ... of the requested type
- 'post_type'=> $post_type,
- // ... and it supports the taxonomy
- 'taxonomy' => $taxonomy,
- // ... without limit
- 'nopaging' => true,
- ));
- // Reset the main query
- wp_reset_query();
- foreach ($query_posts as $query_post) {
- $post_id = $query_post->ID;
- $raw_title = $query_post->post_title;
- // Generate a slug based on the title.
- // We want to check for auto-draft so we don't create a term for a post with no title
- $slug_title = sanitize_title($raw_title);
- // Do the checks for empty titles
- // If the title is blank, skip it
- if ($slug_title == 'auto-draft' || empty($raw_title)) continue;
- // Get all of the terms associated with the post
- $terms = get_the_terms($post_id, $taxonomy);
- $term_id = 0;
- if (!empty($terms)) {
- $term_id = $terms[0]->term_id;
- }
- if ($term_id > 0) {
- // If the post has a term, update the term
- wp_update_term($term_id, $taxonomy, array(
- 'description' => $raw_title,
- 'slug' => $raw_title,
- 'name' => $raw_title,
- ));
- } else {
- // Otherwise add a new term
- wp_set_object_terms($post_id, $raw_title, $taxonomy, false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement