从 SQL 查询中获取类别词元 (WordPress)

Get category term meta from SQL query (WordPress)

我正在使用我在别处找到的这个查询,它运行良好。它基本上 returns 当前作者发布的每个类别的列表:

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 以便它也 returns meta_key meta_value 来自 termmeta table:

数据库中的每个汽车类别都有一个名为 car_statusmeta_key,其值存储在termmeta table,我希望该值显示在 foreach 循环中的描述下方。

这应该适合你

  global $wpdb;
    $categories = $wpdb->get_results("
        SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.description, termmeta.meta_value as car_status, 
        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
        LEFT JOIN $wpdb->termmeta as termmeta ON terms.term_id = termmeta.term_id AND termmeta.meta_key = 'car_status'
        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
    ");
    <?php
    global $wpdb;
    $categories = $wpdb->get_results("
        SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.description, termmeta.meta_key as car_status, termmeta.meta_value  
        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
        LEFT JOIN $wpdb->termmeta as termmeta  ON termmeta.term_id = terms.term_id and termmeta.meta_key = 'car_status'
        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 echo $category->car_status; ?>
    </li>
    <?php endforeach; ?>