Почему не работает вывод похожих постов по рубрике?

Создан тип постов с рубриками
В файле single поставила вывод похожих постов с проверкой рубрики... Но похожие посты не выводятся.
В чем может быть ошибка?

add_action( 'init', 'custom_post_type', 0 );
function cptui_register_my_cpts_terms() {
    $labels = array(
        "name" => __( 'Вопрос' ),
        "singular_name" => __( 'Термин' ),
        "menu_name" => __( 'Вопросы/ответы'),
        "all_items" => __( 'Все вопросы', ),
        "add_new" => __( 'Добавить вопрос'),
        "add_new_item" => __( 'Добавить новый вопрос'),
        "edit_item" => __( 'Редактировать вопрос' ),
        "new_item" => __( 'Новый вопрос' ),
        "view_item" => __( 'Просмотреть вопрос'),
        "view_items" => __( 'Просмотреть вопросы' ),
        "search_items" => __( 'Поиск вопросов'),
        "not_found" => __( 'Вопросы не найдены'),
        "not_found_in_trash" => __( 'Вопросы не найдены в корзине'),
    );
    $args = array(
        "label" => __( 'Термины' ),
        "labels" => $labels,
        "description" => "Термины",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => true,
        "rest_base" => "",
        "menu_icon" => "dashicons-list-view",
        "has_archive" => 'voprosy',
        "show_in_menu" => true,
        "exclude_from_search" => true,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => true,
        "rewrite" => array( "slug" => "voprosy", "with_front" => true ),
        "query_var" => true,
        "supports" => array( "title", "editor", "thumbnail", "custom-fields", "revisions" ),
    );
    register_post_type( "voprosy", $args );
}
add_action( 'init', 'cptui_register_my_cpts_terms' );


//Новая таксономия 
function cptui_register_my_taxes_terms_types() {
    $labels = array(
        "name" => __( 'Рубрики вопросов' ),
        "singular_name" => __( 'Рубрика вопросов' ),
        "all_items" => __( 'Все рубрики вопросов'),
        "edit_item" => __( 'Редактировать рубрику'),
        "add_new_item" => __( 'Добавить новую рубрику' ),
    );
    $args = array(
        "label" => __( 'Рубрика' ),
        "labels" => $labels,
        "public" => true,
        "hierarchical" => true,
        "label" => "Рубрика",
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "query_var" => true,
        "rewrite" => true,
        "show_admin_column" => true,
        "show_in_rest" => true,
        "rest_base" => "",
        "show_in_quick_edit" => true,
    );
    register_taxonomy( "voprosy-types", array( "voprosy" ), $args );
}
add_action( 'init', 'cptui_register_my_taxes_terms_types' );


Вывод похожих в single.php:

<div class="sample-posts">
<h4>Читайте также:</h4>
  <?php
  $categories = get_the_category($post->ID);
  if ($categories) {
    $category_ids = array();
    foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
    $args=array(
      'category__in' => $category_ids,
      'post__not_in' => array($post->ID),
      'showposts' => '5',
      'orderby' => 'rand',
      'ignore_sticky_posts' => '1',
      'no_found_rows' => true,
      'cache_results' => false
    );
    $my_query = new wp_query($args);
    if( $my_query->have_posts() ) {
      echo '<ul>';
      while ($my_query->have_posts()) {
        $my_query->the_post();
        ?>
        <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
        <?php
      }
      echo '</ul>';
    }
    wp_reset_query();
  }
  ?>
</div>
  • Вопрос задан
  • 36 просмотров
Решения вопроса 1
artzolin
@artzolin Куратор тега WordPress
php, WordPress разработка сайтов artzolin.ru
Потому что у вас таксономия, а не категория

$categories = get_the_terms( $post->ID, 'voprosy-types' );

$category_ids = array();
foreach( $categories as $individual_category ) {
	$category_ids[] = $individual_category->term_id;
}

$args = array(
	'post__not_in' => array( $post->ID ),
	'posts_per_page' => '5',
	'orderby' => 'rand',
	'tax_query' => [
		[
			'taxonomy' => 'voprosy-types',
			'field' => 'id',
			'terms' => $category_ids,
		]
	]
);
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
@its2easyy
get_the_category только для встроенных категорий работает, для кастомных нужна get_the_terms
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы