Skip to content

Instantly share code, notes, and snippets.

@wpsmith wpsmith/get_terms_fields.php Secret
Last active Oct 6, 2015

Embed
What would you like to do?
PHP: WordPress get_terms_fields for taxonomy.php
<?php
add_filter( 'get_terms_fields', 'wps_get_terms_fields', 10, 3 );
/** .. Known filter; removed docs for brevity **/
function wps_get_terms_fields( $selects, $args, $taxonomies ) {
if ( 'slug=>name' === $args['fields'] ) {
return array( 't.slug', 't.name', 'tt.count', 'tt.taxonomy' );
}
return $selects;
}
add_filter( 'get_terms_fields_terms', 'wps_get_terms_fields_terms', 10, 4 );
/**
* Filter the given taxonomy's terms for terms with posts (items) of publish post_status.
*
* @param array $terms Array of terms (maybe cached) for the given taxonomy.
* @param string $fields Current fields to return from args fields parameter.
* @param array $taxonomies An array of taxonomies.
* @param array $args An array of get_terms() arguments.
*
* @return array $terms Maybe filtered terms.
*/
function wps_get_terms_fields_terms( $terms, $fields, $taxonomies, $args ) {
if ( 'slug=>name' === $$fields ) {
$_terms = array();
foreach( $terms as $term ) {
$_terms[ $term->slug ] = $term->name;
}
}
return $terms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.