I am trying to make a feed arguments into a wordpress loop conditionally, and hence want to add to arguments based on tags, categories or attributes provided by a filter system.
I can't seem to work out how to add tax_queries to arguments, however, using $arg .=
For example, I want to change the following code from this:
if ($value['priceRange'] == 0 && $value['tags'] == 0 && !$value['type']) {
if(!$value['priceRange']) {
$value['priceRange'] = array(0,1000000);
}
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);
}
if ($value['priceRange'] == 0 && $value['tags'] != 0 && !$value['type']) {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => $value['tags'],
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $value['tags'],
),
array(
'taxonomy' => 'pa_branding',
'field' => 'slug',
'terms' => $value['tags'],
),
),
);
}
To this:
if ($value['priceRange'] == 0 && $value['tags'] == 0 && !$value['type']) {
if(!$value['priceRange']) {
$value['priceRange'] = array(0,1000000);
}
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);
}
if ($value['priceRange'] == 0 && $value['tags'] != 0 && !$value['type']) {
$args .=
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => $value['tags'],
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $value['tags'],
),
array(
'taxonomy' => 'pa_branding',
'field' => 'slug',
'terms' => $value['tags'],
),
),
);
}
I later plan to add a conditional meta_query for price onto this also.
Anyone got any pointers?
Thanks in advance!
I am trying to make a feed arguments into a wordpress loop conditionally, and hence want to add to arguments based on tags, categories or attributes provided by a filter system.
I can't seem to work out how to add tax_queries to arguments, however, using $arg .=
For example, I want to change the following code from this:
if ($value['priceRange'] == 0 && $value['tags'] == 0 && !$value['type']) {
if(!$value['priceRange']) {
$value['priceRange'] = array(0,1000000);
}
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);
}
if ($value['priceRange'] == 0 && $value['tags'] != 0 && !$value['type']) {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => $value['tags'],
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $value['tags'],
),
array(
'taxonomy' => 'pa_branding',
'field' => 'slug',
'terms' => $value['tags'],
),
),
);
}
To this:
if ($value['priceRange'] == 0 && $value['tags'] == 0 && !$value['type']) {
if(!$value['priceRange']) {
$value['priceRange'] = array(0,1000000);
}
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);
}
if ($value['priceRange'] == 0 && $value['tags'] != 0 && !$value['type']) {
$args .=
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => $value['tags'],
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $value['tags'],
),
array(
'taxonomy' => 'pa_branding',
'field' => 'slug',
'terms' => $value['tags'],
),
),
);
}
I later plan to add a conditional meta_query for price onto this also.
Anyone got any pointers?
Thanks in advance!
Share Improve this question edited Mar 6, 2020 at 20:49 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Mar 6, 2020 at 20:47 parvez noorparvez noor 1514 bronze badges1 Answer
Reset to default 1Try this...
<?php
if ( $value[ 'priceRange' ] == 0 && $value[ 'tags' ] == 0 && !$value[ 'type' ] ) {
if ( !$value[ 'priceRange' ] ) {
$value[ 'priceRange' ] = array( 0, 1000000 );
}
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);
}
if ( $value[ 'priceRange' ] == 0 && $value[ 'tags' ] != 0 && !$value[ 'type' ] ) {
$args['tax_query'] = array(
'relation' => 'OR',
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => $value[ 'tags' ],
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $value[ 'tags' ],
),
array(
'taxonomy' => 'pa_branding',
'field' => 'slug',
'terms' => $value[ 'tags' ],
),
);
}
?>