Get the latest posts from the category the current post WordPress?

The user went in the post, for example the category of bedon the right is a sidebar that displays recent products, but I need displayed and the last category of the post, in this case bedding. If the user came to the post with the category of fabric, the sidebar displays recent posts from the category of fabric.

PS If I understand correctly, the theme of which is the conclusion of the sidebar, put the hook where it intercepted the output of the last posts, determine the category of the current post and pass the $args of the function wp get recent posts of the current category, right?
July 2nd 19 at 17:53
1 answer
July 2nd 19 at 17:55
That's the hook, who suddenly come in handy:
add_filter( 'widget_posts_args', 'custom_args');
 custom_args function($args) {
 if ( is_single() ){
 global $post;
 $cats = get_the_category($post->ID);
 $all_ids = array();
 foreach ($cats as $cat_id) {
 $all_ids[] = $cat_id->term_id;
 $args = array(
 'posts_per_page' => 1,
 'no_found_rows' => true,
 'post_status' => 'publish',
 'cat' => $all_ids,
 'ignore_sticky_posts' => true
 return $args;

Find more questions by tags WordPress