I'm trying to build functionality (using the Advanced Custom Fields plugin) to list information in a page template for all custom post types ('Product') that have specified taxonomies.
This is my current stable code, which queries the Products, but only based on the taxonomy terms I hard-code:
$posts = get_posts(array( 'posts_per_page' => 10, 'post_type' => 'company_product', 'tax_query' => array( array( 'taxonomy' => 'taxonomy', 'field' => 'slug', 'terms' => 'product1' ), ));
I've used ACF to set up functionality in the backend to select multiple taxonomies (via a repeater field) that I want to apply to the query, so that the admin can modify which Products are returned based on which taxonomies they have.
I tried this:
$posts = get_posts(array( 'posts_per_page' => 10, 'post_type' => 'company_product', 'tax_query' => array( if( have_rows('category_taxonomies') ): while ( have_rows('category_taxonomies') ) : the_row(); array( 'taxonomy' => 'taxonomy', 'field' => 'slug', 'terms' => the_sub_field('category_taxonomy') ), endwhile; endif; ));
But received this error:
Parse error: syntax error, unexpected 'if' (T_IF), expecting ')' in...
As you can see, I'm trying to add an array to the tax_query for each taxonomy that's selected in the backend, but trying to put the if/while functions inside the array itself causes an error.
I assumed the above method wouldn't work, but PHP isn't my expertise so I'm not sure where to go from here. Any help is appreciated.