提问人:Julia Galden 提问时间:9/2/2023 更新时间:9/10/2023 访问量:137
WP get_terms - 两个分类法,一个具有指定术语
WP get_terms - two taxonomies, one with specified term
问:
我试图弄清楚一个话题,我的印象是解决方案是儿戏,但不知何故我不知道如何结束它。直截了当 - 我有与get_terms相关的代码:
<?PHP
$terms = get_terms([
'taxonomy' => array('job-category'),
'hide_empty' => true,
'orderby' => 'count',
'order' => 'DESC',
]);
usort( $terms, function( $a, $b ) {
$a_ste = (int) get_term_meta( $a->term_id, 'stick_to_end', true );
$b_ste = (int) get_term_meta( $b->term_id, 'stick_to_end', true );
if ($a_ste == $b_ste) return 0;
return ($a_ste < $b_ste) ? -1 : 1;
} );
if ($terms) { //categories exists
foreach ($terms as $category) { ?>
<h2 class="category-title">
<?= $category->name ?>
</h2>
<?PHP endforeach; endif; ?>
一切正常。我从工作类别分类中获取术语并显示它们。
问题是我希望这些显示的工作类别术语只是那些在不同的分类法(工作国家/地区)中给出了非常具体的术语(“德国”)的术语。如何获取?在其他循环中,例如wp_query我主要使用tax_query。但在这里我需要/想坚持get_terms......
所以总而言之 - 我正在寻找一种将get_terms与以下功能一起使用的方法:
- job-category (all terms)
- job-country (only term "germany")
谢谢!
答:
要仅检索与“工作国家/地区”分类法中的“德国”术语关联的“工作类别”术语,请使用 ,不能像使用 那样直接按分类术语进行筛选。但是,您可以通过首先从“工作国家/地区”分类中获取“德国”术语 ID,然后使用它来筛选“工作类别”术语来实现此目的。这是你如何做到的:get_terms
WP_Query
<?php
// Get the 'germany' term ID from the 'job-country' taxonomy
$germany_term = get_term_by('slug', 'germany', 'job-country');
$germany_term_id = $germany_term->term_id;
// Now, use the term ID to filter 'job-category' terms
$terms = get_terms([
'taxonomy' => 'job-category',
'hide_empty' => true,
'orderby' => 'count',
'order' => 'DESC',
'meta_query' => [
[
'key' => 'job-country', // This is assuming you have a custom field for 'job-country'
'value' => $germany_term_id,
],
],
]);
usort($terms, function ($a, $b) {
$a_ste = (int) get_term_meta($a->term_id, 'stick_to_end', true);
$b_ste = (int) get_term_meta($b->term_id, 'stick_to_end', true);
if ($a_ste == $b_ste) return 0;
return ($a_ste < $b_ste) ? -1 : 1;
});
if ($terms) { //categories exist
foreach ($terms as $category) { ?>
<h2 class="category-title">
<?= $category->name ?>
</h2>
<?php endforeach;
}
?>
在此代码中,我们首先从“工作国家/地区”分类中获取“德国”术语 ID,然后在参数中使用它来筛选“工作类别”术语。这样,您只会在“工作国家/地区”分类法中获得与“德国”关联的“工作类别”术语。请确保根据需要将 和 替换为实际分类和自定义字段名称。meta_query
'job-country'
'job-country'
评论
尝试在代码中术语的条件中创建一个循环,类似于下面的循环,以指定 .例如:usort()
if ($terms) {
$specific_terms = array();
foreach ($terms as $category) {
$term_has_relationship = false;
$country_relationship = get_term_meta($category->term_id);
foreach($country_relationship as $key=>$val){
if ($val[0] === 'Germany') {
$term_has_relationship = true;
}
}
if ($term_has_relationship) {
// If the term is related to Germany, add it to the array
$specific_terms[] = $category;
}
}
// Now, you have the terms of 'job-category' that have a relationship with 'Germany' in 'job-country'
// Loop through the specific terms
foreach ($specific_terms as $category) {
?>
<h2 class="category-title">
<?= $category->name ?>
</h2>
<?php endforeach;endif;
?>
根据我对你的问题的了解,我认为这会有所帮助。
您还可以调试代码以更有效地分析 WordPress 中的响应。
打开 wp-config.php 文件。并将以下代码插入到文件中:
// Enable debug mode
define('WP_DEBUG', true);
// Store logs in /wp-content/debug.log
define('WP_DEBUG_LOG', true);
在上面的代码中,除了启用调试外,遇到的所有错误也都存储在日志文件中。该文件位于wp-content/debug.log。
如果您正在进行实时安装,或者您的代码报告了很多错误,您还应该在 wp-config.php 文件中将以下内容设置为 false,
define( 'WP_DEBUG_DISPLAY', false );
这将关闭前端的错误日志记录,并且仅将它们记录在 debug.log 文件中。
评论
要根据(工作国家/地区)的术语从(工作类别)获取术语,您必须查看哪些帖子标记了该特定术语,然后找出分配给这些帖子的工作类别术语。
在get_terms中使用WP_Query和object_ids来获取帖子 ID,您可以在 get_terms 中使用 object_ids 参数来获取与这些帖子关联的工作类别术语。
例如:
<?PHP
$args = array(
'post_type' => 'your_custom_post_type', // Replace with your post type
if not 'post'
'tax_query' => array(
array(
'taxonomy' => 'job-country',
'field' => 'slug',
'terms' => 'germany'
),
),
'fields' => 'ids' // only return post IDs
);
$query = new WP_Query($args);
$post_ids = $query->posts;
$terms = get_terms([
'taxonomy' => array('job-category'),
'hide_empty' => true,
'orderby' => 'count',
'order' => 'DESC',
'object_ids' => $post_ids
]);
usort($terms, ... );
不幸的是,不支持像现在这样。但是,您可以通过 to 获取在“工作-国家/地区”分类中具有特定术语的帖子,然后用于从这些帖子中获取“工作类别”术语来实现您的目标。get_terms()
tax_query
WP_Query
WP_Query
wp_list_pluck()
这是一个示例,未经测试,但我相信会按预期工作。请务必检查您的帖子类型并根据需要进行更新。
<?php
// Get posts that have the 'germany' term in the 'job-country' taxonomy
$query = new WP_Query([
'post_type' => 'post', // replace with your post type, i.e. job, or listing if not post
'posts_per_page' => -1,
'tax_query' => [
[
'taxonomy' => 'job-country',
'field' => 'slug',
'terms' => 'germany',
],
],
]);
// Get the 'job-category' terms from those posts
$terms = [];
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_terms = get_the_terms(get_the_ID(), 'job-category');
if (!empty($post_terms) && !is_wp_error($post_terms)) {
$terms = array_merge($terms, $post_terms);
}
}
wp_reset_postdata();
}
// Remove duplicate terms
$terms = array_unique($terms, SORT_REGULAR);
// Sort the terms
usort($terms, function($a, $b) {
$a_ste = (int) get_term_meta($a->term_id, 'stick_to_end', true);
$b_ste = (int) get_term_meta($b->term_id, 'stick_to_end', true);
if ($a_ste == $b_ste) return 0;
return ($a_ste < $b_ste) ? -1 : 1;
});
// Display the terms
if (!empty($terms)) {
foreach ($terms as $category) { ?>
<h2 class="category-title">
<?= $category->name ?>
</h2>
<?php }
}
?>
此代码首先获取“工作-国家/地区”分类中具有“德国”术语的所有帖子。然后,它从这些帖子中获取“工作类别”术语。之后,它会删除任何重复的术语并对术语进行排序。最后,它显示术语。
评论