Вывод тегов для категории (включая подкатегории)

Добавляем в файл functions.php следующую функцию (код):

// get tags for category (with children)
function get_category_tags($args) {
	global $wpdb;
	
	$tags = $wpdb->get_results
	("
		SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name, null as tag_link
		FROM
			".$wpdb->prefix."posts as p1
			LEFT JOIN ".$wpdb->prefix."term_relationships as r1 ON p1.ID = r1.object_ID
			LEFT JOIN ".$wpdb->prefix."term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
			LEFT JOIN ".$wpdb->prefix."terms as terms1 ON t1.term_id = terms1.term_id,

			".$wpdb->prefix."posts as p2
			LEFT JOIN ".$wpdb->prefix."term_relationships as r2 ON p2.ID = r2.object_ID
			LEFT JOIN ".$wpdb->prefix."term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
			LEFT JOIN ".$wpdb->prefix."terms as terms2 ON t2.term_id = terms2.term_id
		WHERE
			t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
			t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
			AND p1.ID = p2.ID
		ORDER by tag_name
	");
	$count = 0;
	foreach ($tags as $tag) {
		$tags[$count]->tag_link = get_tag_link($tag->tag_id);
		$count++;
	}
	return $tags;
}
// get tags for category (with children)

Вызываем эту функцию после в нужном шаблоне и нужном месте следующим образом:

$args = array(
        'categories'                => '12,13,14'
    );

$tags = get_category_tags($args);

12, 13, 14 — меняете на свои IDшники категорий, которые нужно вам показать.

Также в нашем случае необходимо было сделать вывод именно кастомных постов, по этому текущий код мы еще доработали:

// get tags for category (with children)
function get_category_tags($args) {
	global $wpdb;
	if(array_key_exists('post_type', $args)===false)
	{
		$args['post_type'] = "'post'";
	}
	
	$tags = $wpdb->get_results
	("
		SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name, null as tag_link
		FROM
			".$wpdb->prefix."posts as p1
			LEFT JOIN ".$wpdb->prefix."term_relationships as r1 ON p1.ID = r1.object_ID
			LEFT JOIN ".$wpdb->prefix."term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
			LEFT JOIN ".$wpdb->prefix."terms as terms1 ON t1.term_id = terms1.term_id,

			".$wpdb->prefix."posts as p2
			LEFT JOIN ".$wpdb->prefix."term_relationships as r2 ON p2.ID = r2.object_ID
			LEFT JOIN ".$wpdb->prefix."term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
			LEFT JOIN ".$wpdb->prefix."terms as terms2 ON t2.term_id = terms2.term_id
		WHERE
			t1.taxonomy = 'category' AND p1.post_status = 'publish' AND p1.post_type IN (".$args['post_type'].") AND terms1.term_id IN (".$args['categories'].") AND
			t2.taxonomy = 'post_tag' AND p2.post_status = 'publish' AND p2.post_type IN (".$args['post_type'].")
			AND p1.ID = p2.ID
		ORDER by tag_name
	");
	$count = 0;
	foreach ($tags as $tag) {
		$tags[$count]->tag_link = get_tag_link($tag->tag_id);
		$count++;
	}
	return $tags;
}
// get tags for category (with children)

Вызов функции теперь будет следующий для вывод тегой для кастомных постов:

<?$providers = get_category_tags(['categories' => $category_id, 'post_type' => '"game"']);?>

Использовать все это в теме можно следующим образом:

<?$providers = get_category_tags(['categories' => $category_id, 'post_type' => '"game"']);?>
		<?if(count($providers)):?>
		<nav class="providers">
			<?foreach($providers as $provider):?>
			<a href="<?=$provider->tag_link;?>"><?=$provider->tag_name;?></a>
			<?endforeach;?>
		</nav>
		<?endif;?>

Добавить комментарий