定制SQL查询ORDER BY TERM_ORDER

时间:2013-04-12 作者:izharbuen

我有一个自定义分类法,它在“series”下列出术语,其中包含的帖子也以“media\\u type”下的术语出现,其中media\\u type是term\\u id 16。

我想按terms表中的term\\u order列对列表进行排序,但它不起作用。我做错了什么?

谢谢

    $q_result = $wpdb->get_col("SELECT DISTINCT {$wpdb->terms}.name FROM {$wpdb->terms}
            INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_id = {$wpdb->terms}.term_id
            INNER JOIN {$wpdb->term_relationships} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
            WHERE {$wpdb->term_taxonomy}.taxonomy = \'series\' AND {$wpdb->term_relationships}.object_id IN (
            SELECT object_id FROM {$wpdb->term_relationships}
            INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
            WHERE {$wpdb->term_taxonomy}.taxonomy = \'media_type\' AND {$wpdb->term_taxonomy}.term_id = \'16\'
            ORDER BY {$wpdb->terms}.term_order
);");

1 个回复
SO网友:M Khalid Junaid

在你的情况下,你指的是ORDER BY {$wpdb->terms}.term_order 和中的默认值wordpress wp_terms 表中没有此列term_order 列位于wp_term_relationships 表和您所指的wp_terms 表格收件人ORDER BY 该表中不存在的列,而不是该列,您必须执行以下操作

$q_result = $wpdb->get_col("SELECT DISTINCT {$wpdb->terms}.name FROM {$wpdb->terms}
        INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_id = {$wpdb->terms}.term_id
        INNER JOIN {$wpdb->term_relationships} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
        WHERE {$wpdb->term_taxonomy}.taxonomy = \'series\' AND {$wpdb->term_relationships}.object_id IN (
        SELECT object_id FROM {$wpdb->term_relationships}
        INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
        WHERE {$wpdb->term_taxonomy}.taxonomy = \'media_type\' AND {$wpdb->term_taxonomy}.term_id = \'16\'
        ORDER BY {$wpdb->term_relationships}.term_order ASC
)");

结束

相关推荐

从wpdb-作者/用户目录页面选择

我正在wordpress网站上工作,在那里我创建了一个目录页面(authors.php),其中列出了所有用户和特定的作者元。它工作得很好,但我正试图找出如何只显示填写了所需作者元的用户。我尝试了很多不同的事情,但都没有达到我所需要的。这是我的密码<?php $order = \'user_nicename\'; $users = $wpdb->get_results(\"SELECT * FROM $wpdb->users ORDER BY $order\");