Combining Petar’s function and Tiago’s shortened option syntax, this works:
In template:
//once, in header:
$i = 0;
//anytime I want to run the query:
$i++;
$cat_query = prefix_get_wp_query($i);
while ( $cat_query->have_posts() ) {
$cat_query->the_post();
get_template_part( 'template-parts/posts/content', get_post_type() );
wp_reset_postdata();
}
In functions:
function prefix_get_wp_query( $i ) {
if ( $i > 0 ) {
$cat = "b4_settings_homepage_category_".$i;
$sector = get_option($cat)[0];
}
$cat_args = [
'post_type' => $post_types,
'posts_per_page' => 3,
'fields' => 'ids',
'post__not_in' => [ $feat_ids, $latest_ids, $trend_ids ],
'tax_query' => [
'relation' => 'AND',
[ 'taxonomy' => 'sector',
'field' => 'id',
'terms' => $sector
]
]
];
return new WP_Query( $cat_args );
}
Thanks very much!!