admin管理员组文章数量:1410730
I have a two custom taxonomies:
- Type
- Product
Type
can be the following:
- Blog
- Case study
- Webinar
Subject
can be:
- Indoor
- 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 andsubject
outdoor. - Post 2: Tagged with
type
blog andsubject
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 fortype
.$resource_subject
: Is the variable holding the dropdown value forsubject
.
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 andsubject
indoor and it is showing mesubject
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:
- Blog
- Case study
- Webinar
Subject
can be:
- Indoor
- 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 andsubject
outdoor. - Post 2: Tagged with
type
blog andsubject
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 fortype
.$resource_subject
: Is the variable holding the dropdown value forsubject
.
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 andsubject
indoor and it is showing mesubject
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
1 Answer
Reset to default 2If 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:
When filtering by
type
alone, show onlyresources
posts in thattype
.When filtering by
subject
alone, show onlyresources
posts in thatsubject
.When filtering by
type
andsubject
, show onlyresources
posts which are assigned to bothtype
andsubject
.
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,
),
);
}
本文标签: custom post typesQuerying two taxonomies with taxquery not woking
版权声明:本文标题:custom post types - Querying two taxonomies with tax_query not woking 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745062184a2640297.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论