WordPress: escludere dal Loop i post con un determinato tag

Possiamo escludere i post aventi un determinato tag dal Loop di WordPress.

WordPress di Gabriele Romanato

Possiamo creare un Loop in WordPress che vada ad escludere quei post con un determinato tag. L'unica difficoltà è che il parametro tag__not_in accetta come valore l'ID numerico del tag. Vediamo come fare.

Nel file functions.php creiamo una funzione che, dato il nome (slug) di un tag, restituisce il suo ID:


function get_tag_id_by_name($tag_name) {
	global $wpdb;
	return $wpdb->get_var("SELECT term_id FROM ". $wpdb->terms." WHERE name =  '" . $tag_name . "'");
}

Quindi usiamo tale funzione con il parametro tag__not_in nel nostro tema:


query_posts(
 array(
  'tag__not_in'	=> array(get_tag_id_by_name('tag'))
);

Commenti