【发布时间】:2021-10-24 15:47:04
【问题描述】:
我正在使用我在其他地方找到的这个查询,它可以正常工作。它基本上返回当前作者发布的每个类别的列表:
global $wpdb;
$categories = $wpdb->get_results("
SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.description
FROM $wpdb->posts as posts
LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
WHERE
posts.post_status = 'publish' AND
posts.post_type = 'cars' AND
tax.taxonomy = 'cars_category' AND
posts.post_author = '$user_id'
ORDER BY terms.name ASC
");
foreach($categories as $category) : ?>
<li>
name: <?php echo $category->name; ?>
description: <?php echo $category->description; ?>
status: <?php // status goes here ?>
</li>
<?php endforeach; ?>
但我不知道如何修改 SQL 以便它也返回 termmeta 表中的 meta_key meta_value: p>
数据库中的每个汽车类别都有一个名为 car_status 的 meta_key,其值存储在 termmeta 表中,我想要这样在 foreach 循环中的描述下方显示的值。
【问题讨论】:
-
您首先还需要加入该表,并为该加入创建适当的 ON 条件。
标签: php sql wordpress loops foreach