Skip to content

Instantly share code, notes, and snippets.

@Lonsdale201
Created May 28, 2024 09:30
Show Gist options
  • Save Lonsdale201/67b6ceee55faff4963c95c4dc8f87dcc to your computer and use it in GitHub Desktop.
Save Lonsdale201/67b6ceee55faff4963c95c4dc8f87dcc to your computer and use it in GitHub Desktop.
JetEngine Custom macro - Empty terms
// place the code in the child theme functions.php or a custom code snippets plugin like FluentSnippets
// how to use
// Create a new Terms Query in the Query builder
// Go to the Include/Exclude section
// click the Include section dynamic icon and choose the Empty terms option, and select whitch terms want to use
add_action( 'jet-engine/register-macros', function() {
class Empty_Terms_Macro extends \Jet_Engine_Base_Macros {
public function macros_tag() {
return 'empty_terms';
}
public function macros_name() {
return 'Empty Terms';
}
public function macros_args() {
$taxonomies = get_taxonomies(array('public' => true), 'objects');
$taxonomy_options = array();
foreach ($taxonomies as $taxonomy) {
$taxonomy_options[$taxonomy->name] = $taxonomy->label . ' (' . $taxonomy->name . ')';
}
return array(
'taxonomy' => array(
'label' => 'Taxonomy',
'type' => 'select',
'options' => $taxonomy_options,
'default' => key($taxonomy_options)
),
);
}
public function macros_callback( $args = array() ) {
if ( empty( $args['taxonomy'] ) ) {
return 'No taxonomy specified';
}
$taxonomy = $args['taxonomy'];
$terms = get_terms(array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
));
$empty_terms = array();
foreach ( $terms as $term ) {
if ( $term->count == 0 ) {
$empty_terms[] = $term->term_id;
}
}
if ( empty( $empty_terms ) ) {
return 'No empty terms found';
}
return implode( ',', $empty_terms );
}
}
new Empty_Terms_Macro();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment