Skip to content

Instantly share code, notes, and snippets.

@joelstransky
Last active July 28, 2016 21:53
Show Gist options
  • Save joelstransky/b509b53c51857cf972c31aeff76be5be to your computer and use it in GitHub Desktop.
Save joelstransky/b509b53c51857cf972c31aeff76be5be to your computer and use it in GitHub Desktop.
acf taxonomy meta data lookup
function acf_get_terms( $args = array() ) {
global $wpdb;
$defaults = array(
'taxonomy_slug' => '',
'acf_field_name' => null,
'meta_value' => '',
);
$args = wp_parse_args( $args, $defaults );
if ( empty($args['taxonomy_slug']) || ! taxonomy_exists( $args['taxonomy_slug'] ) || empty( $args['acf_field_name'] ) ) {
return new WP_Error( 'invalid_option_name', __('ACF term meta names are formatted as {$term->taxonomy}_{$term->term_id}_{$field[\'name\']}.') );
}
$rows = $wpdb->get_results($wpdb->prepare(
"
SELECT option_name
FROM {$wpdb->prefix}options
WHERE option_name LIKE %s
AND option_value LIKE %s
",
$args['taxonomy_slug'] . '_%_' . $args['acf_field_name'],
empty($args['meta_value']) ? '%' : $args['meta_value']
));
$unit_ids = array();
foreach( $rows as $row ){
preg_match('/^' . $args['taxonomy_slug'] . '_(\d*)_' . $args['acf_field_name'] . '$/', $row->option_name, $matches);
$unit_ids[] = $matches[1];
}
if ( empty($rows) ) {
$terms = array();
} else {
$terms = get_terms(array(
'taxonomy' => $args['taxonomy_slug'],
'hide_empty' => false,
'include' => $unit_ids,
));
}
return $terms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment