0

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.

1

OK, I have no idea how and why your code should work... It has nothing in common with correct PHP syntax... But it's pretty good pseudo-code, so I think I can guess, what you wanted to achieve...

$tax_query = array();
if ( have_rows('category_taxonomies') ) {
    while ( have_rows('category_taxonomies') ) {
        the_row();
        $tax_query[] = array(
            'taxonomy' => 'taxonomy',  // <- you should put real taxonomy name in here
            'field' => 'slug',
            'terms' => get_sub_field('category_taxonomy')
        );
    }
}

$posts = get_posts( array(
    'posts_per_page' => 10,
    'post_type'      => 'company_product',
    'tax_query'      => $tax_query
));
-1

If conditions don't run inside of arrays, so you will need to write separate arrays for each condition:

$posts = get_posts(if( have_rows('category_taxonomies') ){array(
'posts_per_page'    => 10,
'post_type'         => 'company_product',
'tax_query' => array('taxonomy' => 'taxonomy',
                    'field' => 'slug',
                    'terms' => the_sub_field('category_taxonomy')),
   }else{array(
   'posts_per_page'    => 10,
   'post_type'         => 'company_product'
   )}
));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.