gpt4 book ai didi

WP get_terms - two taxonomies, one with specified term(WP GET_TERMS-两个分类,一个具有指定的术语)

转载 作者:bug小助手 更新时间:2023-10-27 20:23:10 26 4
gpt4 key购买 nike



I'm trying to figure out one topic, I have the impression that the solution is child's play, but somehow I don't know how to end it. To the point - I have code related to get_terms:

我在试图弄清楚一个话题,我的印象是解决方案是儿童游戏,但不知何故,我不知道如何结束它。切中要害-我有与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; ?>

Everything works ok. I fetch the terms from the job-category taxonomy and display them.

一切正常。我从工作类别分类中获取术语并显示它们。


The issue is that I want these displayed job-category terms to be only those that have very specific terms ("germany") given in a different taxonomy (job-country). How to get it? in other loops like wp_query I mostly used tax_query. But here I need/want to stick to get_terms...

问题是,我希望这些显示的职务类别术语只是那些在不同的分类(职务-国家/地区)中具有非常特定的术语(“德国”)的术语。怎样才能得到它?在wp_Query等其他循环中,我主要使用了TAX_QUERY。但在这里我需要/想要坚持Get_Terms.


So just to sum up - I'm looking for a way to use get_terms with:

总而言之,我正在寻找一种方法来使用Get_Terms:


- job-category (all terms)
- job-country (only term "germany")

Thank you!

谢谢!


更多回答

Have you taken a look at the $args['taxonomy'] and the $args['include'] parameters of get_terms()?

您看过Get_Terms()的$args[‘soronomy’]和$args[‘Include’]参数了吗?

yes i've did. And i didnt find anything that works :(

是的我试过了。我没有找到任何有用的东西:

优秀答案推荐

To retrieve only the 'job-category' terms that are associated with the 'germany' term in the 'job-country' taxonomy using get_terms, you can't directly filter by taxonomy terms as you would with WP_Query. However, you can achieve this by first getting the 'germany' term ID from the 'job-country' taxonomy and then using it to filter the 'job-category' terms. Here's how you can do it:

要使用GET_TERMS仅检索与‘JOB-COUNTRY’分类中的‘德国’术语相关联的‘JOB-CATEGORY’术语,您不能像使用WP_QUERY那样直接按分类术语进行筛选。但是,您可以通过以下方式实现这一点:首先从“工作-国家”分类中获取“德国”术语ID,然后使用它过滤“工作-类别”术语。以下是你如何做到这一点:


<?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;
}
?>

In this code, we first obtain the 'germany' term ID from the 'job-country' taxonomy and then use it in the meta_query parameter to filter the 'job-category' terms. This way, you'll only get 'job-category' terms that are associated with 'germany' in the 'job-country' taxonomy. Make sure to replace 'job-country' and 'job-country' with your actual taxonomy and custom field names as needed.

在这段代码中,我们首先从‘JOB-COUNTRY’分类中获取‘德国’术语ID,然后在META_QUERY参数中使用它来筛选‘JOB-CATEGORY’术语。这样,您将只获得与“工作国家/地区”分类中的“德国”相关的“工作类别”术语。确保根据需要用您的实际分类和自定义字段名称替换‘JOB-COUNTRY’和‘JOB-COUNTRY’。



Try creating a loop within the conditional for the terms in your code, similar to the one below, to specify the job-country just below the usort(). For example:

尝试在代码中术语的条件内创建一个循环,类似于下面的循环,以指定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;
?>

From what I understand of your question, I think this would help.

根据我对您问题的理解,我认为这会有所帮助。


You can also debug the code to analyze the responses more effectively in WordPress.

您还可以调试代码,以便在WordPress中更有效地分析响应。


Open the wp-config.php file. And insert the following code into the file:

打开wp-config.php文件。并将以下代码插入到文件中:


// Enable debug mode
define('WP_DEBUG', true);
// Store logs in /wp-content/debug.log
define('WP_DEBUG_LOG', true);

In the above code, in addition to enabling debugging, all encountered errors are also stored in a log file. The file is located at wp-content/debug.log.

在上面的代码中,除了启用调试之外,所有遇到的错误也都存储在日志文件中。该文件位于wp-content/debug.log。


If you are working on a live installation, or your code is reporting a lot of bugs, you should also set the following to false in your wp-config.php file,

如果您正在进行实时安装,或者您的代码报告了许多错误,您还应该在wp-config.php文件中将以下代码设置为FALSE,


define( 'WP_DEBUG_DISPLAY', false );

this will switch off the error logging on the front-end and only log them in the debug.log file.

这将关闭前端上的错误记录,并仅将它们记录在调试.log文件中。



To get terms from (job-category) based on a term from (job-country), you have to see which posts are tagged with that specific term and then find out which job-category terms are assigned to those posts.

要根据术语从(职务-国家)获取术语,您必须查看哪些帖子标记了该特定术语,然后找出哪些职务类别术语被分配给这些帖子。


Use WP_Query with object_ids in get_terms to get the post IDs, you can utilize the object_ids parameter in get_terms to fetch the job-category terms associated with these posts.

将WP_QUERY与GET_TERMS中的OBJECT_ID一起使用获取帖子ID,您可以使用GET_TERMS中的OBJECT_IDs参数来获取与这些帖子关联的职务类别术语。


For example:

例如:


<?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, ... );


Unfortunately, get_terms() does not support tax_query like WP_Query does. However, you can achieve your goal by using WP_Query to get the posts that have the specific term in the 'job-country' taxonomy and then use wp_list_pluck() to get the 'job-category' terms from those posts.

遗憾的是,Get_Terms()不像WP_Query那样支持TAX_QUERY。然而,您可以通过使用wp_Query来获取在‘职位-国家’分类中具有特定术语的职位,然后使用wp_list_pluck()从这些职位中获得‘职位-类别’术语来实现您的目标。


Here is a sample, untested but I believe will work as expected. Be sure to check your post type and update as needed.

这是一个未经测试的样本,但我相信它会像预期的那样工作。请务必检查您的帖子类型,并根据需要进行更新。


<?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 }
}
?>

This code first gets all the posts that have the 'germany' term in the 'job-country' taxonomy. Then it gets the 'job-category' terms from those posts. After that, it removes any duplicate terms and sorts the terms. Finally, it displays the terms.

这段代码首先获取在‘职位-国家’分类中具有‘德国’术语的所有帖子。然后,它会从这些帖子中获取“工作类别”术语。之后,它会删除所有重复的术语并对这些术语进行排序。最后,它显示术语。


更多回答

unfortunately this code doesn't work as expected. My initial code was grouping posts with specific taxonomy terms (category1->post a, post b etc). Your code do not show any of the taxonomy terms, just a list of all posts and without matching both taxonomies

不幸的是,这段代码不能像预期的那样工作。我最初的代码是使用特定的分类术语对帖子进行分组(类别1->帖子a、帖子b等)。您的代码没有显示任何分类法术语,只显示了所有帖子的列表,并且没有匹配两个分类法

It returns no posts at all

它根本不返回任何帖子

I edited the previous resolution to search for all terms and then filter by searched in the conditional just to validate if there is a relationship in the taxonymy informed by you. Also enable logs and use var_dump() to validate method returns and better analyze.

我编辑了前面的解析以搜索所有术语,然后在条件中按搜索进行过滤,只是为了验证您通知的分类法中是否存在关系。还要启用日志并使用var_ump()来验证方法返回并更好地进行分析。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com