Site icon Hip-Hop Website Design and Development

Trouble inputting variable into WP query

Not sure the best way to describe this, but basically I am defining a variable ($cat) which chooses a taxonomy based on a number defined just before the query is called, but as the number is defined AFTER the args I can’t figure out how to get it to work. I know I could just duplicate the whole query for each taxonomy I want, but I was hoping there was a less character-heavy way to do this.

Current code:

//cat changer (using a settings page so non-dev colleagues can switch taxonomies on the homepage)
if ( $cat == 1 ) { $sector = get_option('b4_settings_homepage_category_1')[0]; }
if ( $cat == 2 ) { $sector = get_option('b4_settings_homepage_category_2')[0]; }
if ( $cat == 3 ) { $sector = get_option('b4_settings_homepage_category_3')[0]; }
if ( $cat == 4 ) { $sector = get_option('b4_settings_homepage_category_4')[0]; }
if ( $cat == 5 ) { $sector = get_option('b4_settings_homepage_category_5')[0]; }
if ( $cat == 6 ) { $sector = get_option('b4_settings_homepage_category_6')[0]; }
if ( $cat == 8 ) { $sector = get_option('b4_settings_homepage_category_7')[0]; }
if ( $cat == 9 ) { $sector = get_option('b4_settings_homepage_category_8')[0]; }
if ( $cat == 10 ) { $sector = get_option('b4_settings_homepage_category_9')[0]; }
if ( $cat == 11 ) { $sector = get_option('b4_settings_homepage_category_10')[0]; }
if ( $cat == 12 ) { $sector = get_option('b4_settings_homepage_category_11')[0]; }
if ( $cat == 13 ) { $sector = get_option('b4_settings_homepage_category_12')[0]; }
if ( $cat == 14 ) { $sector = get_option('b4_settings_homepage_category_13')[0]; }
//that 'get_option' outputs as the plain ID of the taxonomy

//args & query
$args_cat = array( 
   'post_type' => $post_types,
   'posts_per_page' => 3,
   'fields' => 'ids',
   'post__not_in' => array( $feat_ids, $latest_ids, $trend_ids ),
   'tax_query' => array( 
      'relation' => 'AND',
      array(
         'taxonomy' => 'sector',
         'field' => 'id',
         'terms' => $sector
      )
   )
);
$cat_query = new WP_Query( $args_cat );

//choosing the category and hoping to run the query with it (not working)
$cat = 1;
while ( $cat_query->have_posts() ) { 
   $cat_query->the_post();
   get_template_part( 'template-parts/posts/content', get_post_type() );
   wp_reset_postdata();
}

So it’s like I’m wanting it to go back up to the top of the page and choose the cat based on the number, then run the query with that option, but I just can’t figure it out. Running it as-is returns ‘terms’ => NULL. Do I need a reset or a return or something?

Any help greatly appreciated.

By the way, I know I could do it like this for each tax/query:

$sector = get_option('b4_settings_homepage_category_1')[0];
$args_cat = array( 
   'post_type' => $post_types,
   'posts_per_page' => 3,
   'fields' => 'ids',
   'post__not_in' => array( $feat_ids, $latest_ids, $trend_ids ),
   'tax_query' => array(
      'relation' => 'AND',
      array( 
         'taxonomy' => 'sector',
         'field' => 'id',
         'terms' => $sector
      )
   )
);
$cat_query = new WP_Query( $args_cat );
while ( $cat_query->have_posts() ) {
$cat_query->the_post(); 
etc etc

But I really don’t want to have that query replicated a bunch of times if it can be avoided…