Using a global variable will work. Here’s a demonstration:
function wpse_shortcode_function( $atts ){
// User provided values are stored in $atts.
// Default values are passed to shortcode_atts() below.
// Merged values are stored in the $a array.
$a = shortcode_atts( [
'id' => false,
], $atts );
// Set global variable $value using value of shortcode's id attribute.
$GLOBALS['value'] = $a['id'];
// Add our filter and do a query.
add_filter( 'posts_where', 'wpse_filter_value' );
$my_query = new WP_Query( [
'p' => $GLOBALS['value'],
] );
if ( $my_query->have_posts() ) {
while ( $my_query->have_posts() ) {
$my_query->the_post();
the_title( '<h1>', '</h1>');
}
wp_reset_postdata();
}
// Disable the filter.
remove_filter( 'posts_where', 'wpse_filter_value' );
}
add_shortcode( 'my-shortcode', 'wpse_shortcode_function' );
function wpse_filter_value( $where ){
// $GLOBALS['value'] is accessible here.
// exit ( print_r( $GLOBALS['value'] ) );
return $where;
}
Side note, declaring a function within another function is not a good practice.