0

我想从 Wordpress 数据库中选择内容来填充所有不同类别的 <select> ,但有可能 2 个或更多帖子具有相同的类别,在这种情况下,我应该只显示一次该类别。

一个例子:

Post1 cat = EN
Post2 cat = EN
Post3 cat = DE
Post4 cat = FR
Post5 cat = FR

// The Result sould look like this:
EN
DE
FR

这是代码的当前状态:(但效果不佳)

 <select name="sprache">
        <option value="0">Alle Sprachen</option>
        <?php

        $selectsql = " 
            SELECT  
            wpos ts.*,
            wterms.*

            FROM $wpdb->posts wposts
                    INNER JOIN $wpdb->term_relationships AS tr ON (wposts.ID = tr.object_id)
                    INNER JOIN $wpdb->term_taxonomy AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
                    INNER JOIN $wpdb->terms wterms ON (tt.term_id = wterms.term_id)
                    INNER JOIN $wpdb->postmeta wpostmeta ON (wposts.ID = wpostmeta.post_id)

            WHERE wposts.post_status = 'publish'
            AND tt.taxonomy = 'th5'
            AND wpostmeta.meta_key= '_sprachgruppen'

            GROUP BY tt.taxonomy 
            ";

 $select_output = $wpdb->get_results($selectsql,OBJECT);

 if ($select_output){ 
    foreach ($select_output as $post){
        setup_postdata($post);

   if(isset($_POST[gruppe])){
        $option .= '<option value="'.$post->term_id.'"';
        $option .= ' >';
        $option .= $post->name;
        $option .= '</option>';

   }
 echo $option;
4

1 回答 1

0

这里有一个类别列表post_type

 SELECT `name` FROM`wp_terms` WHERE `term_id` IN (
 SELECT term_id FROM `wp_term_taxonomy` WHERE `term_taxonomy_id` IN 
 (SELECT `term_taxonomy_id` FROM `wp_term_relationships` WHERE `object_id` IN 
 (SELECT ID FROM `wp_posts` WHERE post_type='your custom post type') AND `object_id` IN
 (SELECT post_id FROM `wp_postmeta` WHERE meta_key='_sprachgruppe')
 )
 )

如果知道确切的会更容易term_taxonomy name

于 2013-04-09T14:12:25.877 回答