Skip to content

Instantly share code, notes, and snippets.

@axxe16
Created March 13, 2020 10:01
Show Gist options
  • Save axxe16/7dd7b0d65e651c57f182f00c3c2c45c1 to your computer and use it in GitHub Desktop.
Save axxe16/7dd7b0d65e651c57f182f00c3c2c45c1 to your computer and use it in GitHub Desktop.
ACF - restituisce lista autori in base a genere letterario #wp_query #query #custom post #taxonomy #custom taxonomy #acf
<?php
//restituisce lista autori in base a genere letterario
//dato id autore recupero genere e stampo lista escluso autore corrente
function get_author_by_genre($id_autore) {
$generi = get_field('genere_letterario', get_queried_object());
$args = array(
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
'exclude' => $id_autore, //escludo la tassonomia corrente (dato il suo id)
//effettuo una query in cui si filtrano i risultati in base a valori multipli (genere letterario)
'meta_query' => array(
'relation' => 'OR',
)
);
foreach($generi as $genere) {
array_push($args['meta_query'], array(
'key' => 'genere_letterario',
'value' => '"' . $genere . '"',
'compare' => 'LIKE'
));
}
$terms = get_terms('autori', $args);
$str = "<div class=\"listTag\">\n";
foreach( $terms as $term ) {
$str .= "<a class=\"badge badge-light\" href=\"". get_term_link( $term ) ."\">". $term->name ."</a>\n";
}
$str .= "</div>\n";
wp_reset_query();
return $str;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment