admin管理员组文章数量:1130349
I have a function set up to show 'Similar Products', i.e. showing products that share the same products-category taxonomy term. This works great but I want to narrow the function even further and make it more specific. Thus I want it to look at 2 taxonomies (product-category and space) to find similar products. The current markup as follows:
<?php
$terms = wp_get_post_terms( $post->ID, 'products-category' );
if($terms){
// post has course_type terms attached
$course_terms = array();
foreach ($terms as $term){
$course_terms[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query(
array(
'posts_per_page' => '4',
'post_type' => 'regularproducts',
'tax_query' => array(
array(
'taxonomy' => 'products-category',
'field' => 'slug',
'terms' => $course_terms, //the taxonomy terms I'd like to dynamically query
),
),
'orderby' => 'title',
'order' => 'ASC',
)
);
if ( have_posts() ): ?>
<?php while (have_posts() ) : the_post(); ?> //etc...
So it currently only looks at the products-category taxonomy for similar products (posts), but I want it to look at BOTH product-category and space to display more specific similar products, if possible. Any suggestions would be greatly appreciated!
I have a function set up to show 'Similar Products', i.e. showing products that share the same products-category taxonomy term. This works great but I want to narrow the function even further and make it more specific. Thus I want it to look at 2 taxonomies (product-category and space) to find similar products. The current markup as follows:
<?php
$terms = wp_get_post_terms( $post->ID, 'products-category' );
if($terms){
// post has course_type terms attached
$course_terms = array();
foreach ($terms as $term){
$course_terms[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query(
array(
'posts_per_page' => '4',
'post_type' => 'regularproducts',
'tax_query' => array(
array(
'taxonomy' => 'products-category',
'field' => 'slug',
'terms' => $course_terms, //the taxonomy terms I'd like to dynamically query
),
),
'orderby' => 'title',
'order' => 'ASC',
)
);
if ( have_posts() ): ?>
<?php while (have_posts() ) : the_post(); ?> //etc...
So it currently only looks at the products-category taxonomy for similar products (posts), but I want it to look at BOTH product-category and space to display more specific similar products, if possible. Any suggestions would be greatly appreciated!
2 Answers
Reset to default 16First of all, get all term slugs from the custom taxonomy space by current post ID.
$space_terms = wp_get_post_terms( $post->ID, 'space' );
if( $space_terms ) {
$space_terms = array();
foreach( $space_terms as $term ) {
$space_terms[] = $term->slug;
}
}
You should specify the logical relationship between each inner taxonomy array when there is more than one.
The relation key in the array describes the relationship. Possible values are OR and AND.
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'products-category',
'field' => 'slug',
'terms' => $course_terms,
),
array(
'taxonomy' => 'space',
'field' => 'slug',
'terms' => $space_terms,
),
),
Simply get all terms first: Here i used property custom post type for example
$property_statu_terms = wp_get_post_terms( $post->ID, 'property_status', array( 'fields' => 'ids' ));
$property_type_terms = wp_get_post_terms( $post->ID, 'property_type', array( 'fields' => 'ids' ));
$property_city_terms = wp_get_post_terms( $post->ID, 'property_city', array( 'fields' => 'ids' ));
$property_state_terms = wp_get_post_terms( $post->ID, 'property_state', array( 'fields' => 'ids' ));
$property_feature_terms = wp_get_post_terms( $post->ID, 'property_feature', array( 'fields' => 'ids' ));
Use relation [OR] or [AND] to get your desired posts.
$query = new WP_Query( array(
'post_type' => 'property',
'posts_per_page' => 3,
'orderby' => 'rand',
'post__not_in' => array( $post->ID ),
'ignore_sticky_posts' => 1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'property_status',
'field' => 'id',
'terms' => $property_statu_terms,
),
array(
'taxonomy' => 'property_type',
'field' => 'id',
'terms' => $property_type_terms,
),
array(
'taxonomy' => 'property_city',
'field' => 'id',
'terms' => $property_city_terms,
),
array(
'taxonomy' => 'property_state',
'field' => 'id',
'terms' => $property_state_terms,
),
array(
'taxonomy' => 'property_feature',
'field' => 'id',
'terms' => $property_feature_terms,
),
),
));
I have a function set up to show 'Similar Products', i.e. showing products that share the same products-category taxonomy term. This works great but I want to narrow the function even further and make it more specific. Thus I want it to look at 2 taxonomies (product-category and space) to find similar products. The current markup as follows:
<?php
$terms = wp_get_post_terms( $post->ID, 'products-category' );
if($terms){
// post has course_type terms attached
$course_terms = array();
foreach ($terms as $term){
$course_terms[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query(
array(
'posts_per_page' => '4',
'post_type' => 'regularproducts',
'tax_query' => array(
array(
'taxonomy' => 'products-category',
'field' => 'slug',
'terms' => $course_terms, //the taxonomy terms I'd like to dynamically query
),
),
'orderby' => 'title',
'order' => 'ASC',
)
);
if ( have_posts() ): ?>
<?php while (have_posts() ) : the_post(); ?> //etc...
So it currently only looks at the products-category taxonomy for similar products (posts), but I want it to look at BOTH product-category and space to display more specific similar products, if possible. Any suggestions would be greatly appreciated!
I have a function set up to show 'Similar Products', i.e. showing products that share the same products-category taxonomy term. This works great but I want to narrow the function even further and make it more specific. Thus I want it to look at 2 taxonomies (product-category and space) to find similar products. The current markup as follows:
<?php
$terms = wp_get_post_terms( $post->ID, 'products-category' );
if($terms){
// post has course_type terms attached
$course_terms = array();
foreach ($terms as $term){
$course_terms[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query(
array(
'posts_per_page' => '4',
'post_type' => 'regularproducts',
'tax_query' => array(
array(
'taxonomy' => 'products-category',
'field' => 'slug',
'terms' => $course_terms, //the taxonomy terms I'd like to dynamically query
),
),
'orderby' => 'title',
'order' => 'ASC',
)
);
if ( have_posts() ): ?>
<?php while (have_posts() ) : the_post(); ?> //etc...
So it currently only looks at the products-category taxonomy for similar products (posts), but I want it to look at BOTH product-category and space to display more specific similar products, if possible. Any suggestions would be greatly appreciated!
-
Add another array in
'tax_query' => array(this should solve your problem. – SLH Commented Nov 9, 2014 at 21:13 -
@SLH yes but what about
$terms = wp_get_post_terms( $post->ID, 'products-category' );as this is only using theproduct-categorytaxonomy term? – user1374796 Commented Nov 9, 2014 at 21:29 -
Then create other variable $space_terms and get the terms by post ID for your second taxonomy.
array( 'taxonomy' => 'space', 'field' => 'slug', 'terms' => $space_terms, ),– SLH Commented Nov 9, 2014 at 21:32 - @SLH ok, unless its something I'm doing wrong...would you possibly be able to put the full code in an answer so I can test? – user1374796 Commented Nov 9, 2014 at 21:51
2 Answers
Reset to default 16First of all, get all term slugs from the custom taxonomy space by current post ID.
$space_terms = wp_get_post_terms( $post->ID, 'space' );
if( $space_terms ) {
$space_terms = array();
foreach( $space_terms as $term ) {
$space_terms[] = $term->slug;
}
}
You should specify the logical relationship between each inner taxonomy array when there is more than one.
The relation key in the array describes the relationship. Possible values are OR and AND.
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'products-category',
'field' => 'slug',
'terms' => $course_terms,
),
array(
'taxonomy' => 'space',
'field' => 'slug',
'terms' => $space_terms,
),
),
Simply get all terms first: Here i used property custom post type for example
$property_statu_terms = wp_get_post_terms( $post->ID, 'property_status', array( 'fields' => 'ids' ));
$property_type_terms = wp_get_post_terms( $post->ID, 'property_type', array( 'fields' => 'ids' ));
$property_city_terms = wp_get_post_terms( $post->ID, 'property_city', array( 'fields' => 'ids' ));
$property_state_terms = wp_get_post_terms( $post->ID, 'property_state', array( 'fields' => 'ids' ));
$property_feature_terms = wp_get_post_terms( $post->ID, 'property_feature', array( 'fields' => 'ids' ));
Use relation [OR] or [AND] to get your desired posts.
$query = new WP_Query( array(
'post_type' => 'property',
'posts_per_page' => 3,
'orderby' => 'rand',
'post__not_in' => array( $post->ID ),
'ignore_sticky_posts' => 1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'property_status',
'field' => 'id',
'terms' => $property_statu_terms,
),
array(
'taxonomy' => 'property_type',
'field' => 'id',
'terms' => $property_type_terms,
),
array(
'taxonomy' => 'property_city',
'field' => 'id',
'terms' => $property_city_terms,
),
array(
'taxonomy' => 'property_state',
'field' => 'id',
'terms' => $property_state_terms,
),
array(
'taxonomy' => 'property_feature',
'field' => 'id',
'terms' => $property_feature_terms,
),
),
));
本文标签: custom post typesquery multiple taxonomies
版权声明:本文标题:custom post types - query multiple taxonomies 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749227998a2335931.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


'tax_query' => array(this should solve your problem. – SLH Commented Nov 9, 2014 at 21:13$terms = wp_get_post_terms( $post->ID, 'products-category' );as this is only using theproduct-categorytaxonomy term? – user1374796 Commented Nov 9, 2014 at 21:29array( 'taxonomy' => 'space', 'field' => 'slug', 'terms' => $space_terms, ),– SLH Commented Nov 9, 2014 at 21:32