custom post types - Querying two taxonomies with tax_query not woking

I have a two custom taxonomies:TypeProductType can be the following:BlogCase studyWebinarSubject can be:IndoorOutdoo

I have a two custom taxonomies:

  • Type
  • Product

Type can be the following:

  1. Blog
  2. Case study
  3. Webinar

Subject can be:

  1. Indoor
  2. Outdoor

I have two drop down menus in WordPress backend where a user can select what type and subject they want to show from resources (my custom post type).

Say, for example, the following posts exist in resources:

  • Post 1: Tagged with type blog and subject outdoor.
  • Post 2: Tagged with type blog and subject indoor.
  • Post 3: Tagged with subject indoor.

The user can either filter by type or subject. By this I mean not both are required, only one can be selected. But, if the user does choose both a type and subject, I want it to display posts with both tags.

Current approach:

$args = array(  
  'post_type' => 'resources',
  'post_status' => 'publish',
  'posts_per_page' => 8,
  'tax_query' => array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'type',
        'field'    => 'slug',
        'terms'    => $resource_type,
    ),
    array(
        'taxonomy' => 'subject',
        'field'    => 'slug',
        'terms'    => $resource_subject,
    ),
  ),
  'tax_query' => array(
    'relation' => 'OR',
    array(
        'taxonomy' => 'type',
        'field'    => 'slug',
        'terms'    => $resource_type,
    ),
    array(
        'taxonomy' => 'subject',
        'field'    => 'slug',
        'terms'    => $resource_subject,
    ),
  )

);
  • $resource_type: Is the variable holding the dropdown value for type.
  • $resource_subject: Is the variable holding the dropdown value for subject.

Current results:

  • When filtering by subject alone - it works.

  • When filtering by type alone - it works.

  • When filtering with both - it doesn't work. I.e. I've filtered by type blog and subject indoor and it is showing me subject outdoor posts.

Alternatively, the following works when querying both, but does not work when only choosing one:

 $args = array(  
    'post_type' => 'resources',
    'post_status' => 'publish',
    'posts_per_page' => $card_count,
    'tax_query' => array(
      'relation' => 'AND',
      array(
          'taxonomy' => 'type',
          'field'    => 'slug',
          'terms'    => $resource_type,
      ),
      array(
          'taxonomy' => 'subject',
          'field'    => 'slug',
          'terms'    => $resource_subject,
      ),
    )

  );

I have a two custom taxonomies:

  • Type
  • Product

Type can be the following:

  1. Blog
  2. Case study
  3. Webinar

Subject can be:

  1. Indoor
  2. Outdoor

I have two drop down menus in WordPress backend where a user can select what type and subject they want to show from resources (my custom post type).

Say, for example, the following posts exist in resources:

  • Post 1: Tagged with type blog and subject outdoor.
  • Post 2: Tagged with type blog and subject indoor.
  • Post 3: Tagged with subject indoor.

The user can either filter by type or subject. By this I mean not both are required, only one can be selected. But, if the user does choose both a type and subject, I want it to display posts with both tags.

Current approach:

$args = array(  
  'post_type' => 'resources',
  'post_status' => 'publish',
  'posts_per_page' => 8,
  'tax_query' => array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'type',
        'field'    => 'slug',
        'terms'    => $resource_type,
    ),
    array(
        'taxonomy' => 'subject',
        'field'    => 'slug',
        'terms'    => $resource_subject,
    ),
  ),
  'tax_query' => array(
    'relation' => 'OR',
    array(
        'taxonomy' => 'type',
        'field'    => 'slug',
        'terms'    => $resource_type,
    ),
    array(
        'taxonomy' => 'subject',
        'field'    => 'slug',
        'terms'    => $resource_subject,
    ),
  )

);
  • $resource_type: Is the variable holding the dropdown value for type.
  • $resource_subject: Is the variable holding the dropdown value for subject.

Current results:

  • When filtering by subject alone - it works.

  • When filtering by type alone - it works.

  • When filtering with both - it doesn't work. I.e. I've filtered by type blog and subject indoor and it is showing me subject outdoor posts.

Alternatively, the following works when querying both, but does not work when only choosing one:

 $args = array(  
    'post_type' => 'resources',
    'post_status' => 'publish',
    'posts_per_page' => $card_count,
    'tax_query' => array(
      'relation' => 'AND',
      array(
          'taxonomy' => 'type',
          'field'    => 'slug',
          'terms'    => $resource_type,
      ),
      array(
          'taxonomy' => 'subject',
          'field'    => 'slug',
          'terms'    => $resource_subject,
      ),
    )

  );
Share Improve this question edited Oct 22, 2019 at 9:12 Freddy asked Oct 22, 2019 at 7:55 FreddyFreddy 1771 silver badge12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

If you add another tax_query to the args, the last tax_query will replace the previous tax_query.

But anyway, if these are the criteria:

  1. When filtering by type alone, show only resources posts in that type.

  2. When filtering by subject alone, show only resources posts in that subject.

  3. When filtering by type and subject, show only resources posts which are assigned to both type and subject.

Then try this:

$args = array(
    'post_type'      => 'resources',
    'post_status'    => 'publish',
    'posts_per_page' => 8,
);

// Filtering by both 'type' and 'subject'.
if ( $resource_type && $resource_subject ) {
    $args['tax_query'] = array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'type',
            'field'    => 'slug',
            'terms'    => $resource_type,
        ),
        array(
            'taxonomy' => 'subject',
            'field'    => 'slug',
            'terms'    => $resource_subject,
        ),
    );
}
// Filtering by 'type' only.
elseif ( $resource_type && ! $resource_subject ) {
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'type',
            'field'    => 'slug',
            'terms'    => $resource_type,
        ),
    );
}
// Filtering by 'subject' only.
elseif ( $resource_subject ) {
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'subject',
            'field'    => 'slug',
            'terms'    => $resource_subject,
        ),
    );
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745062184a4609030.html

相关推荐

  • custom post types - Querying two taxonomies with tax_query not woking

    I have a two custom taxonomies:TypeProductType can be the following:BlogCase studyWebinarSubject can be:IndoorOutdoo

    17小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信