array_map('absint', $cat_ids), // Sanitize IDs 'post_type' => $current_post_type, 'post__not_in' => [$post_id], 'posts_per_page' => 3, 'no_found_rows' => true, // Skip pagination count 'ignore_sticky_posts' => true, // Ignore sticky posts ]; // Cache query results $cache_key = 'related_posts_' . md5(serialize($query_args)); $related_cats_post = wp_cache_get($cache_key); if (false === $related_cats_post) { $related_cats_post = new WP_Query($query_args); wp_cache_set($cache_key, $related_cats_post, '', 3600); // Cache for 1 hour } // Output related posts if ($related_cats_post->have_posts()) : ?>

have_posts()) : $related_cats_post->the_post(); ?>