Code Snippets

Not sure if something like this already exists in WP

function get_post_ids_satisfying_taxonomy_terms($taxterms) {
 
  $first_pass = true;
 
  foreach($taxterms as $taxonomy => $term_slug) {
    $the_term = get_term_by('slug',$term_slug,$taxonomy);
    //pp($the_term);
    $object_ids = get_objects_in_term($the_term->term_id,Array($taxonomy));
 
    if ($first_pass) {
      $results = $object_ids;
      $first_pass = false;
    }
    else {    //accumulate the post ids which satisfy all taxonomy => term pairs (AND, not OR)
      $results = array_intersect($results,$object_ids);  
    }
  }
 
 
  $results = empty($results) ? Array(-1) : $results; //the post__in query_var gets ignored on an empty array, so return an invalid post id in an array
 
  return $results;
}

This snippet will change all categories above 3 to be sub-categories of 3

    require_once(ABSPATH . 'wp-admin/includes/taxonomy.php');
    pp(get_categories('hide_empty=0'));
    foreach (get_categories('hide_empty=0') as $c) {
      if ($c->term_id > 3) {      
        $result = wp_insert_category(
          Array(
          'cat_ID' => $c->term_id,
          'category_parent' => 3,
          'cat_name' => $c->name
          ),true);
 
        pp($result);
      }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *