Skip to content

Instantly share code, notes, and snippets.

@RadGH
Created June 13, 2019 02:12
Show Gist options
  • Save RadGH/8fb9941f6a01a3f222a1620807f5ee45 to your computer and use it in GitHub Desktop.
Save RadGH/8fb9941f6a01a3f222a1620807f5ee45 to your computer and use it in GitHub Desktop.
WordPress: Get all terms used by the given post ids
<?php
/**
* Returns an array of terms used by the list of post IDs.=
* By default returns an array of arrays, each with the keys: term_id, slug, name, term_taxonomy_id
* You can get an array of single values by changing $return to one of: term_id, slug, name, term_taxonomy_id
*
* @param $post_ids
* @param $taxonomy
* @param string $return
*
* @return array
*/
function dtl_faq_get_terms_for_post_ids( $post_ids, $taxonomy, $return = 'all' ) {
global $wpdb;
// Required values
if ( empty($post_ids) || !$taxonomy ) return array();
// Ensure post IDs are all value integers
if ( !is_array($post_ids) ) $post_ids = (array) $post_ids;
$post_ids = array_map('intval', $post_ids);
$post_ids = array_filter($post_ids);
// Join post IDs to a comma separated string, and make sure that is escaped
$id_string = esc_sql( implode(', ', $post_ids) );
// Customize the SELECT statement and change what rows are returned.
$select = "term.term_id, term.slug, term.name, tx.term_taxonomy_id";
if ( $return == 'term_id' ) $select = 'term.term_id';
else if ( $return == 'slug' ) $select = 'term.slug';
else if ( $return == 'name' ) $select = 'term.name';
else if ( $return == 'term_taxonomy_id' ) $select = 'tx.term_taxonomy_id';
// Build the SQL query.
$sql = <<<MYSQL
select distinct {$select}
from {$wpdb->term_relationships} tr
inner join {$wpdb->term_taxonomy} tx
on tx.term_taxonomy_id = tr.term_taxonomy_id
inner join {$wpdb->terms} term
on term.term_id = tx.term_id
where
tx.taxonomy = %s
and
tr.object_id in ({$id_string})
limit 100;
MYSQL;
// Fill in the taxonomy
$sql = $wpdb->prepare( $sql, $taxonomy );
if ( $return === 'all' ) {
// Get an array of arrays as the result when using "all"
return $wpdb->get_results( $sql, ARRAY_A );
}else{
// Otherwise get an array of single values
return $wpdb->get_col( $sql );
}
}
@RadGH
Copy link
Author

RadGH commented Jun 13, 2019

Examples:

// Where:
$post_ids = array(42610, 42612, 42613);
$taxonomy = "faq-category";

dtl_faq_get_terms_for_post_ids( $post_ids, $taxonomy );

array(2) {
  [0]=>
  array(4) {
    ["term_id"]=>
    string(3) "910"
    ["slug"]=>
    string(8) "renewals"
    ["name"]=>
    string(8) "Renewals"
    ["term_taxonomy_id"]=>
    string(3) "910"
  }
  [1]=>
  array(4) {
    ["term_id"]=>
    string(3) "911"
    ["slug"]=>
    string(5) "other"
    ["name"]=>
    string(5) "Other"
    ["term_taxonomy_id"]=>
    string(3) "911"
  }
}

dtl_faq_get_terms_for_post_ids( $post_ids, $taxonomy, 'name' );

array(2) {
  [0]=>
  string(8) "Renewals"
  [1]=>
  string(5) "Other"
}

dtl_faq_get_terms_for_post_ids( $post_ids, $taxonomy, 'term_id' );

array(2) {
  [0]=>
  string(3) "910"
  [1]=>
  string(3) "911"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment