You should use get_queried_object()
or get_queried_object_id()
to get the current custom post’s ID. IMO get_queried_object()
is a better choice here as you can conditionaly check for the post type also. Try this
function wpse258217_random_posts() {
$obj = get_queried_object();
if ( $obj->post_type === 'question' ) {
$postid = $obj->ID;
$my_query = new WP_Query( array('showposts' => '8', 'post_type' => 'question', 'post__not_in' => array( $postid ), 'orderby' => 'rand'));
//Execute the loop here
}
}