我不知道该如何表达我的问题,所以我希望你能理解。
我有两个自定义的分类法,其中包含不同的术语。
让我们给他们打电话custom_tax1
&;custom_tax2
.
custom_tax1
term1
term2
term3
...
custom_tax2
term1
term2
term3
...
Question:是否可以列出custom_tax2
, 贴子标记为term1
从…起custom_tax1
?我不知道该如何表达我的问题,所以我希望你能理解。
我有两个自定义的分类法,其中包含不同的术语。
让我们给他们打电话custom_tax1
&;custom_tax2
.
custom_tax1
term1
term2
term3
...
custom_tax2
term1
term2
term3
...
Question:是否可以列出custom_tax2
, 贴子标记为term1
从…起custom_tax1
?我不确定是否可以直接查询这些术语,因此我将为您编写一个解决方法。
这个WP_Query();
接受名为terms
. 可以将术语ID作为数组传递给此参数。
让我们首先获取第一个分类中的术语列表,然后根据这些术语进行查询:
$terms = get_terms(
array(
\'taxonomy\' => \'custom_tax1\',
\'hide_empty\' => false,
\'fields\' => \'ids\', // We only need the IDs
)
);
// Now do a query
$args = array(
\'tax_query\' => array(
array(
\'taxonomy\' => \'custom_tax2\',
\'field\' => \'slug\',
\'terms\' => $terms,
),
),
);
$my_query = new WP_Query( $args );
这将查询来自custom_tax2
有以下任何条款custom_tax1
. 现在您已经有了包含这些术语的帖子,您可以继续从中提取术语。让我们更进一步地解决这个问题:
if($my_query->have_posts()){
// Define an empty array to use later
$post_terms = array();
while($my_query->have_posts()){
$my_query->the_post();
// Merge the previous terms with the new ones
$new_terms = wp_get_post_terms( get_the_ID(), \'custom_tax1\');
$post_terms = array_merge( $post_terms, $new_terms );
}
}
下面是发生的事情。我们在所有属于custom_tax2
具有以下术语的分类法custom_tax2
分类法
我需要为大约2500篇文章添加一个类别,或者需要使用SQL查询,或者wp_set_object_terms 使用PHP。我在看wp_set_object_terms 因此,我可以添加一个类别,而不会影响当前分配的类别。下面使用的函数wp_set_object_terms\' (来自WordPress Codex)将数组中的一个或多个类别(在示例中为类别6和8)添加到ID为42的帖子(在示例中)。但我需要能够添加一个post id数组($object_id), 不是类别数组。根据https://codex.