0

I need to generate a sorted array of all the terms within a set of (six) taxonomies (custom tags) for a custom post type without any duplicates (of which there are likely to be many).

Is this something get_terms() can handle and if so, how do I go about doing that? If not, is my best approach 6 calls to get_terms() followed by an array_merge?

1

You have all sorts of fun questions today! There is also a WP_Term_Query available in WordPress. Here is some info on it as well as what parameters you can pass it. https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/

$term_args = array(
  'taxonomy' => array('tax_one', 'tax_two', 'tax_six'),
  'hide_empty' => false,
  'fields' => 'all',
  'count' => true,
);

$term_query = new WP_Term_Query($term_args);

foreach ( $term_query->terms as $term ) {
  // Here is what lives inside of $term when the 'field' parameter above is set to all
  /*
  WP_Term Object (
      [term_id] => 11
      [name] => General Topics
      [slug] => general-topics
      [term_group] => 0
      [term_taxonomy_id] => 11
      [taxonomy] => category
      [description] => 
      [parent] => 0
      [count] => 22
      [filter] => raw
  )
  */
  // Maybe build out your array() in here to NOT have duplicate Terms ??
}

Hope that helps!!

  • 1
    That is very helpful, thank you. On a side note, there may be more questions like this - I'm developing a plugin which is proving to be much more work than I first imagined. I am learning so much about WordPress though so it is all good. – Matthew Brown aka Lord Matt Jun 13 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.