I suppose you need to make it global. It might look like:
add_action( 'pre_get_posts', 'do_user_post_selection' );
function do_user_post_selection( $query ) {
if( !is_home() || !is_user_logged_in() )
return;
if( isset( $query->query_vars['post_type'] ) && ( 'nav_menu_item' == $query->query_vars['post_type'] ) )
return;
if( 1 < did_action('wp') )
return;
global $current_user, $tax_selection;
$tax_selection = get_user_option( 'taxonomy_selection', $current_user->ID );
if( empty( $tax_selection ) )
return;
if( isset( $tax_selection['categories'] ) && ( is_array( $tax_selection['categories'] ) ) && ( !empty( $tax_selection['categories'] ) ) )
$query->set( 'category__in', $tax_selection['categories'] );
if( isset( $tax_selection['post_tag'] ) && ( is_array( $tax_selection['post_tag'] ) ) && ( !empty( $tax_selection['post_tag'] ) ) )
$query->set( 'tag__in', $tax_selection['post_tag'] );
return;
}
front-page.php
<?php // Create and run custom loop
global $tax_selection; // use your variable
$custom_posts = new WP_Query();
$custom_posts->query( array(
'post_type' => 'topic',
'posts_per_page' => 5,
'category__in' => $tax_selection['categories'],
) );
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="entry-left">
<h3><a href="https://wordpress.stackexchange.com/questions/77121/<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
</div><!-- .entry-header -->
</article>
<?php endwhile; ?>
</div>