[英]Get the current page custom taxonomy use to query custom post type
So far I have a custom taxonomy that is registered called position
that is associated with a specific page as well a custom post type called People
. 到目前为止,我有一个自定义分类法,该分类法被注册为与特定页面相关联的称为
position
,以及一个名为People
的自定义帖子类型。 I also have a page that has the custom taxonomy set to use the position
taxonomy as well. 我还有一个页面,该页面已将自定义分类法设置为也使用
position
分类法。
What I am trying to do is get it so that I can query the custom post type of People
to show a list of entries with the custom taxonomy that is set on the page as well as the post type. 我想做的就是获取它,以便我可以查询
People
的自定义帖子类型,以显示具有在页面上设置的自定义分类法以及帖子类型的条目列表。
For example, the taxonomy of position
has some items assigned to it, eg - Sales, Accounting, etc. I have some entries for the custom post type of People
eg - John, Mary, Sam, etc. I have John and Mary set to use the taxonomy of Sales
, in this case. 例如,分类学
position
分配了一些项目,例如: -销售,会计,等我有自定义职位类型的某些条目People
如-约翰,玛丽,山姆等我有约翰和玛丽设为在这种情况下,请使用Sales
的分类法。
On the page of Sales, I have it set to use the custom position
taxonomy of Sales
. 在Sales页面上,我设置为使用
Sales
的自定义position
分类法。
What I'm trying to get is get the custom taxonomy of position
on the Sales template page which should return the name and/or slug of Sales
and then take that info and pass it into a query to check against the People
custom post type and pull the associated people from that custom taxonomy. 我想要得到的是获得的自定义分类
position
销售模板页面上应返回的名称和/或蛞蝓Sales
,然后采取这一信息,并把它传递到一个查询所要检查的People
自定义后的类型和从相关的习惯分类中删除相关人员。 In this case, on the Sales page it should be able to pull the two people of John and Mary. 在这种情况下,应该可以在“销售”页面上拉开约翰和玛丽的两个人。
So far I have this on the Sales template page: 到目前为止,我在“销售”模板页面上具有以下内容:
<?php $tax = get_terms( array( 'taxonomy' => 'position' ) ); ?>
That gives me the array of the terms but how do I get the name of the associated taxonomy name that is set on the page and then pass that to a query to get the people? 这给了我一系列的术语,但是如何获得在页面上设置的相关分类法名称的名称,然后将其传递给查询以获取人员呢?
Looks like if I use get_terms
it gives me everything and just need the one set taxonomy that is for that particular page, assign that to a variable and then use it for a query like this: 看起来,如果我使用
get_terms
它可以为我提供一切,并且只需要用于该特定页面的一组分类法即可,将其分配给变量,然后将其用于如下查询:
<?php $args = array( 'post_type' => 'people', 'taxonomy' => $tax);
$loop = new WP_Query( $args ); ?>
Ok so I got it figured out eventually. 好的,我最终弄清楚了。
In order to make this work you'd need to get the current post ID from the page, pass that through get_the_terms
and then construct a query to query the custom post type with tax_query
and then pass that into the tax_query
array: 为了完成这项工作,您需要从页面获取当前的帖子ID,将其传递给
get_the_terms
,然后构造一个查询以使用tax_query
查询自定义帖子类型,然后将其传递给tax_query
数组:
<?php
$id = get_post($post->ID);
$tax = get_the_terms($id, 'position');
$args = array(
'post_type' => 'people',
'tax_query' => array(
array(
'taxonomy' => 'position',
'field' => 'slug',
'terms' => $tax[0]->slug,
),
),
);
$loop = new WP_Query( $args );
?>
That will cross reference the custom taxonomy between the current page and the custom post types and will get those post types that are assigned with the specific taxonomy (in this case, Sales). 这将在当前页面和自定义帖子类型之间交叉引用自定义分类,并获得使用特定分类(在这种情况下为Sales)分配的那些帖子类型。
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.