<?php
$current_post_id = get_the_ID();

// Получаем категории текущего поста
$categories = get_the_category($current_post_id);
if ($categories) {
    $category_ids = array_map(function ($category) {
        return $category->term_id;
    }, $categories);

    $args = array(
        'category__in'   => $category_ids,
        'post__not_in'   => array($current_post_id),
        'posts_per_page' => 4,
    );

    $related_posts_query = new WP_Query($args);

    if ($related_posts_query->have_posts()) {

        echo '<div class="article-section__title">Читайте также:</div>';
        echo '<div class="articles-preview">';

        while ($related_posts_query->have_posts()) {
            $related_posts_query->the_post();
            get_template_part('content', 'post');
        }

        echo '</div>';
        wp_reset_postdata();
    }
}
?>