最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - How to add tax_query to $args with concatenation

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

Try 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' ],
          ),
    );

}
?>
发布评论

评论列表(0)

  1. 暂无评论