Функция для вывода категорий таксономии с постами в ней WordPress
<?php
/*
Если $whith_posts == true - выводить посты связанные с термином(категорией) таксономии
Если $whith_posts == false - выводить только термины аксономии
*/
function vin_get_uslugi_terms($whith_posts = false)
{
$terms = get_terms(
[
'taxonomy' => 'katalog_uslug', /*здесь Ваша таксономия*/
'hide_empty' => true,
]);
if (!$whith_posts)
{
return $terms;
}
elseif ($whith_posts)
{
$arrResult = [];
foreach ($terms as $key => $term):
$term_id=$term->term_id;
$term_item=$term->name;
$res = new WP_Query(array(
'tax_query' =>
array(
array(
'taxonomy' => 'katalog_uslug',
'field' => 'id',
'terms' => $term_id
)
)));
$term->posts= $res->posts;
$arrResult[$term_id]= $term;
endforeach;
return $arrResult;
}
}
/*На выходе получаем такой массив*/
/*
array(2) {
[8]=>
object(WP_Term)#6177 (11) {
["term_id"]=>
int(8)
["name"]=>
string(25) "Другие услуги"
["slug"]=>
string(13) "drugie-uslugi"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(8)
["taxonomy"]=>
string(13) "katalog_uslug"
["description"]=>
string(0) ""
["parent"]=>
int(0)
["count"]=>
int(1)
["filter"]=>
string(3) "raw"
["posts"]=>
array(1) {
[0]=>
object(WP_Post)#6121 (24) {
["ID"]=>
int(150)
["post_author"]=>
string(1) "1"
["post_date"]=>
string(19) "2021-12-09 12:24:14"
["post_date_gmt"]=>
string(19) "2021-12-09 09:24:14"
["post_content"]=>
string(0) ""
["post_title"]=>
string(27) "Другая услуга 1"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(4) "open"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(0) ""
["post_name"]=>
string(16) "drugaya-usluga-1"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2021-12-09 12:24:14"
["post_modified_gmt"]=>
string(19) "2021-12-09 09:24:14"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(58) "http://local-maniulator333.ru/?post_type=usluga&p=150"
["menu_order"]=>
int(0)
["post_type"]=>
string(6) "usluga"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
}
}
[7]=>
object(WP_Term)#6176 (11) {
["term_id"]=>
int(7)
["name"]=>
string(58) "Перевозка грузов манипулятором"
["slug"]=>
string(31) "perevozka-gruzov-manipulyatorom"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(7)
["taxonomy"]=>
string(13) "katalog_uslug"
["description"]=>
string(0) ""
["parent"]=>
int(0)
["count"]=>
int(3)
["filter"]=>
string(3) "raw"
["posts"]=>
array(3) {
[0]=>
object(WP_Post)#6125 (24) {
["ID"]=>
int(149)
["post_author"]=>
string(1) "1"
["post_date"]=>
string(19) "2021-12-09 09:06:10"
["post_date_gmt"]=>
string(19) "2021-12-09 06:06:10"
["post_content"]=>
string(0) ""
["post_title"]=>
string(81) "Перевозка кирпича, пеноблоков, досок, срубов"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(4) "open"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(0) ""
["post_name"]=>
string(42) "perevozka-kirpicha-penoblokov-dosok-srubov"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2021-12-09 09:07:40"
["post_modified_gmt"]=>
string(19) "2021-12-09 06:07:40"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(58) "http://local-maniulator333.ru/?post_type=usluga&p=149"
["menu_order"]=>
int(0)
["post_type"]=>
string(6) "usluga"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
[1]=>
object(WP_Post)#6172 (24) {
["ID"]=>
int(148)
["post_author"]=>
string(1) "1"
["post_date"]=>
string(19) "2021-12-09 09:05:54"
["post_date_gmt"]=>
string(19) "2021-12-09 06:05:54"
["post_content"]=>
string(0) ""
["post_title"]=>
string(90) "Монтаж манипулятором срубов, металлоконструкций"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(4) "open"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(0) ""
["post_name"]=>
string(49) "montazh-manipulyatorom-srubov-metallokonstrukczij"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2021-12-09 09:07:44"
["post_modified_gmt"]=>
string(19) "2021-12-09 06:07:44"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(58) "http://local-maniulator333.ru/?post_type=usluga&p=148"
["menu_order"]=>
int(0)
["post_type"]=>
string(6) "usluga"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
[2]=>
object(WP_Post)#6154 (24) {
["ID"]=>
int(147)
["post_author"]=>
string(1) "1"
["post_date"]=>
string(19) "2021-12-09 09:05:34"
["post_date_gmt"]=>
string(19) "2021-12-09 06:05:34"
["post_content"]=>
string(0) ""
["post_title"]=>
string(76) "Перевозка бытовок, киосков манипулятором"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(4) "open"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(0) ""
["post_name"]=>
string(40) "perevozka-bytovok-kioskov-manipulyatorom"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2021-12-09 09:07:49"
["post_modified_gmt"]=>
string(19) "2021-12-09 06:07:49"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(58) "http://local-maniulator333.ru/?post_type=usluga&p=147"
["menu_order"]=>
int(0)
["post_type"]=>
string(6) "usluga"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
}
}
}
*/