A
A
Alexandra2021-10-18 18:24:43
WordPress
Alexandra, 2021-10-18 18:24:43

Why does the display of similar posts by category not work?

Created type of posts with headings
In the single file, I set the output of similar posts with a heading check... But similar posts are not displayed.
What could be wrong?

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' );


Similar output in 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>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Zolin, 2021-10-18
@Aleksa-s

Because you have a taxonomy, not a category

$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,
    ]
  ]
);

I
its2easyy, 2021-10-18
@its2easyy

get_the_category only works for built-in categories, for custom ones you need get_the_terms

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question