Come filtrare i post per tassonomia usando AJAX
Ho trovato questo post che descrive come filtrare i post per categoria con Ajax e funziona benissimo, ma voglio anche filtrare le mie tassonomie personalizzate allo stesso modo e non riesco a farlo funzionare. Mi mostra tutti i post invece di mostrare solo quelli della mia tassonomia.
So che il menu deve essere modificato da get_the_categories
a get_the_terms
, ma ho bisogno specificamente di aiuto su cosa modificare nella funzione jQuery e nella funzione PHP alla fine. Ho provato ad aggiungere una tax_query
richiamando la mia tassonomia ma continua a non mostrare i post corretti. Qualcuno può aiutarmi a indicarmi la direzione giusta?

Ho capito! Ecco il codice che ho utilizzato:
Aggiungi al functions.php
:
add_action( 'wp_ajax_nopriv_load-filter2', 'prefix_load_term_posts' );
add_action( 'wp_ajax_load-filter2', 'prefix_load_term_posts' );
function prefix_load_term_posts () {
$term_id = $_POST[ 'term' ];
$args = array (
'term' => $term_id,
'posts_per_page' => -1,
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'yourtaxonomyhere',
'field' => 'id',
'terms' => $term_id,
'operator' => 'IN'
)
)
);
global $post;
$myposts = get_posts( $args );
ob_start (); ?>
<ul class="list">
<?php foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php echo get_post_meta($post->ID, 'image', $single = true); ?></a><br />
<?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata();
$response = ob_get_contents();
ob_end_clean();
echo $response;
die(1);
}
Script jQuery:
<script>
function term_ajax_get(termID) {
jQuery("a.ajax").removeClass("current");
jQuery("a.ajax").addClass("current"); //aggiunge la classe current all'elemento del menu categoria visualizzato per poterlo stilare con css
jQuery("#loading-animation").show();
var ajaxurl = 'http://yourdomain.com/wp-admin/admin-ajax.php';
jQuery.ajax({
type: 'POST',
url: ajaxurl,
data: {"action": "load-filter2", term: termID },
success: function(response) {
jQuery("#category-post-content").html(response);
jQuery("#loading-animation").hide();
return false;
}
});
}
</script>
Non sto usando una funzione per elencare le categorie, le sto semplicemente elencando separatamente. Sostituisci il numero con l'ID del tuo termine:
<ul class="nav">
<li id="term-166"><a class="yourtermname ajax" onclick="term_ajax_get('166');" href="#">Nome del tuo termine</a></li>
<li id="term-354"><a class="yourtermname ajax" onclick="term_ajax_get('354');" href="#">Nome del tuo termine</a></li>
</ul>
Inoltre, se vuoi filtrare i tag invece dei termini, sostituisci:
'term'
con'tag__in'
,$term_id
con$tag_id
- e cambia
'taxonomy' => 'yourtaxonomyhere'
in'taxonomy' => 'post_tag'
.

Ti suggerisco di utilizzare uno shortcode per visualizzare la tassonomia a tua scelta: crea una classe per dichiarare lo shortcode e richiama questa funzione
public function shortcode($atts)
{
extract(shortcode_atts( array(
'data' => 'taxonomy',
'taxonomy' => 'category',
//argomenti get_terms
'parent' => 0, //ottiene solo i termini di primo livello di default
'exclude'=>'',
'type'=>'radio' // checkbox,radio
), $atts,'astSearchInput' ));
$arrStr =array();
$arrStr[]= "<div class='astSearchInput " . $taxonomy. "' taxonomy='" .$taxonomy. "'>" ;
if ($type=="checkbox" || $type=="radio")
{
if ($data=="taxonomy")
{
//echo $datatata;
$arrValues=get_terms($taxonomy, array("parent"=>$parent, "exclude"=>$exclude));
}
if ($type=="checkbox")$arrStr[]= $this->inputCheckBox($arrValues,$atts);
if ($type=="radio")$arrStr[]= $this->inputRadio($arrValues,$atts);
}
$arrStr[]= "</div>";
$str=join("\n",$arrStr);
return $str ;
}
function inputCheckBox($arrValues,$attr)
{
$arrStr =array();
$arrStr[]='<div class="formcb">';
foreach($arrValues as $k=>$term)
{
$title=$term->name; //$term->description
// print_r($term);
$id="cb" . $term->term_id;
$arrStr[]='<div class="cb"><input class="astInpuntTerm astcheckBox" type="checkbox" id="' . $id .'" value="' . $term->term_id . '" ><label for="' . $id . '">' . $title. '</label></div>';
}
$arrStr[]='</div>';
$str=join("\n",$arrStr);
return $str;
}
http://www.webmasterbulletin.net/wordpress-ajax-taxonomy-search-shortcode

Ho avuto un problema simile.
Il codice è buono, ma necessita di una piccola modifica per funzionare.
$args = array (
'term' => $term_id,
'posts_per_page' => -1,
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'yourtaxonomyhere', // Inserisci qui la tua tassonomia
'field' => 'id',
'terms' => $term_id,
'operator' => 'IN'
)
),
'post_type' => 'yourcustomposttype', // <== questo mancava
'posts_per_page' => 10,
'order' => 'DESC'
);
