<?php
/*
Plugin Name: Текст
Description: Текст
Author: Текст
*/

add_filter( 'the_content', 'wfm_related_posts' );


function wfm_related_posts($content){

	if( !is_single() ) return $content;

	$id = get_the_ID();
	$categories = get_the_category( $id );

	foreach($categories as $category){
		$cats_id[] = $category->cat_ID;
	}
	
	$related_posts = new WP_Query(
		array(
			'orderby' => 'title',
			'order' => ASC,
			'posts_per_page' => 4,
			'category__in' => $cats_id,
			'post__not_in' => array($id)
		)
	);

	if( $related_posts->have_posts() ){

		while( $related_posts->have_posts() ){
			$related_posts->the_post();
			$content .= '<a href="' . get_permalink() . '">' . get_the_title() . '</a><br>';
		}

		$content .= '</div>';
		wp_reset_query();
	}

	return $content;
}