如何获取与帖子相关的所有分类法

如何获取与帖子相关的所有分类法

本文介绍了如何获取与帖子相关的所有分类法-Wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您抽出宝贵的时间查看此问题,希望能为您提供帮助!

Thanks for taking the time to look at this question and hopefully you can help!

我正在寻找一个wordpress查询来检索与另一个问题相关的所有分类法。

I'm looking to run a wordpress query which retrieves all taxonomies related to another.

例如;说我在Wordpress网站中有一个产品的类别/分类法和一个子产品的类别/分类法。当我进入产品类别/分类页面时,我希望看到所有相关子类别/分类的列表。

For example; say I have a category/taxonomy of products and a category/taxonomy of sub products in my Wordpress site. When I land on a product category/taxonomy page, I would like to see a list of all the related sub categories/taxonomies.

我希望这是有道理的,因为经过数小时的谷歌搜索,我所能找到的只是问题,询问如何获取与分类法相关的所有帖子-并非相反!

I hope this makes sense as after many hours spent googling, all I can find are questions asking how to get all posts related to a taxonomy - not the other way round!

非常感谢!

帕特里克

推荐答案

如果我理解这个问题,则希望获得适用于特定页面上所列产品的分类法和术语,例如

If I understand the question, you would like to get the taxonomies and terms that apply to the products listed on a particular page, e.g. a category or taxonomy page.

假设您有一个商品ID $ product_id的数组,例如当前显示的商品ID。您应该能够获得适用于这些产品的分类法和术语,如下所示:

Suppose that you have an array of product IDs $product_id, for example the IDs of products currently being displayed. You should be able to get the taxonomies and terms that apply to those products as follows:

global $wpdb;

$ids = implode(',', $product_id);
$query = "SELECT x.taxonomy, t.name FROM wp_term_taxonomy AS x
          JOIN wp_term AS t on t.term_id = x.term_id
          JOIN wp_terms_relationships AS r on r.term_taxonomy_id = x.term_taxonomy_id
          WHERE r.object_id IN ($ids)
          GROUP BY x.taxonomy, t.name";
$taxonomies = $wpdb->get_results( $query );

在上面:

SELECT x.taxonomy, t.name FROM wp_term_taxonomy AS x

是分类法(名称)的基本表及其术语的term_id。

is the base table of taxonomies (names) and the term_ids of their terms.

JOIN wp_terms AS t on t.term_id = x.term_id

将wp_term_taxonomy中的每个term_id与其术语名称进行匹配。

matches each of the term_ids from wp_term_taxonomy with its term name.

JOIN wp_term_relationships AS r on r.term_taxonomy_id = x.term_taxonomy_id

然后将其与wp_term_relationships中的条目匹配,该条目显示哪些产品(自定义帖子ID)使用这些条款。

then matches that with entries in wp_term_relationships which shows which products (custom post IDs) use those terms.

WHERE r.object_id IN ($ids)

将列表限制为仅与以下产品相关的术语您的列表。

limits the list to only those terms relevant for the products in your list.

由于在此示例中,我们要求输入x.taxonomy和t.name,

Since, in this example, we are asking for x.taxonomy and t.name,

GROUP BY x.taxonomy, t.name

确保您没有重复的内容。

makes sure that you don't have duplicates. You could get additional fields if you needed by changing the SELECT.

因此,一旦运行get_results(),就应该有一个对象数组,每个对象都有一个分类法名称和术语名称。通用方案为:

So, once you run get_results(), you should have an array of objects, each with a taxonomy name and term name. The general scheme will be:

+-----------+-------+
| taxonomy1 | term1 |
+-----------+-------+
| taxonomy1 | term2 |
+-----------+-------+
| taxonomy1 | term1 |
+-----------+-------+
| taxonomy2 | term1 |
+-----------+-------+
| taxonomy3 | term1 |
+-----------+-------+
...

您现在可以使用此方法为访问者创建一种选择其他子集的方式,例如通过为每个分类法创建术语下拉菜单。

You can now use this to create a way for visitors to select a further subset, e.g. by creating a dropdown of terms for each taxonomy.

这篇关于如何获取与帖子相关的所有分类法-Wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 05:34