2011-03-18 59 views
0

我這是需要收到正確的信息提供給高級定製菜單在WordPress的項目五個表加入5桌PHP SQL

這些都是五個表和列我需要

wp_term_taxonomy   - Need: term_id, taxonomy WHERE: taxonomy="nav_menu" 
wp_terms     - Need: term_id, name WHERE: term_id matches wp_term_taxonomy.term_id 
wp_term_relationships - Need: object_id, term_taxonomy_id WHERE: term_taxonomy_id matches wp_term_taxonomy.term_id 
wp_postmeta    - Need: post_id, meta_key, meta_value WHERE: post_id matches wp_term_relationships.object_id AND meta_key="_menu_item_object_id" 
wp_posts     - Need: id, post_title, post_status, guid, post_parent, post_type WHERE: id matches wp_postmeta.meta_value 

But that is not it I then need to: 

wp_posts     - Need: guid, post_parent, post_type WHERE: post_parent matches wp_posts.id AND post_type="attachment" 
wp_postmeta    - Need: post_id, meta_key, meta_value WHERE: post_id matches wp_posts.id AND meta_key="description" 

我希望這樣做有點意義。

我想要做的是基本上,創建一個下拉菜單,其中包含WordPress自定義菜單功能中的頁面列表,帶走每個頁面的精選圖像,以及他們的自定義字段說明,其中我有一個小文本顯示。

最終的菜單看起來是這個造型:

到目前爲止,我已經有一個更迭使菜單的工作,但不是一個非常好的類型的代碼:

<ul> 
<?php 
    $getMenus = mysql_query('SELECT term_id, taxonomy FROM wp_term_taxonomy WHERE taxonomy="nav_menu"'); 
    while ($addMenus = mysql_fetch_assoc($getMenus)) { 
     $menus_id = $addMenus['term_id']; 
?> 
    <?php 
     $getTerms = mysql_query('SELECT term_id, name FROM wp_terms WHERE term_id='.$menus_id.''); 
     while ($addTerms = mysql_fetch_assoc($getTerms)) { 
    ?> 
     <li> 
      <span class="menu-sub-headline"><?php echo $addTerms['name']; ?></span> 
      <ul> 
       <?php 
        $getTermsRelationship = mysql_query('SELECT object_id, term_taxonomy_id FROM wp_term_relationships WHERE term_taxonomy_id='.$menus_id.''); 
        while ($addTermsRelationship = mysql_fetch_assoc($getTermsRelationship)) { 

        $termsRelationship = $addTermsRelationship['object_id']; 

        $getMetaRelationship = mysql_query('SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id='.$termsRelationship.' and meta_key="_menu_item_object_id"'); 
        while ($addMetaRelationship = mysql_fetch_assoc($getMetaRelationship)) { 

        $metaKeyValue = $addMetaRelationship['meta_value']; 
       ?> 
       <?php 
        $result = mysql_query('SELECT id, post_title, post_status, guid, post_parent FROM wp_posts WHERE id='.$metaKeyValue.''); 
        while ($row = mysql_fetch_assoc($result)) { 
       ?> 
       <li> 
       <span><a href="<?php echo $row['guid']; ?>"><?php echo $row['post_title']; ?></a></span> 
       <?php $thumb = $row['id']; ?> 
       <ul class="menu-sub-sub-item-ul"> 
        <li> 
         <span class="menu-product-headline"><?php echo $row['post_title']; ?></span> 
         <?php $getThumb = mysql_query('SELECT guid, post_parent, post_type FROM wp_posts WHERE post_parent='.$thumb.' AND post_type="attachment"'); 
          while ($addThumb = mysql_fetch_assoc($getThumb)) { 
         ?> 
          <img src="<?php echo $addThumb['guid']; ?>"/> 
         <? } ?> 
         <?php $getMeta = mysql_query('SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id='.$thumb.' AND meta_key="description"'); 
          while ($addMeta = mysql_fetch_assoc($getMeta)) { 
         ?> 
          <p><?php echo $addMeta['meta_value']; ?></p> 
          <a href="<?php echo $row['guid']; ?>"><img src="/wp-content/themes/mygind-co/images/design/read_more.png"/></a> 
         <?php } ?> 
        </li> 
       </ul> 
       <?php }}} ?>  
      </ul> 
     </li> 
    <? } ?> 
<?php } ?> 
</ul> 

希望你們中的一些人可以幫助我達到相同的結果,但是用更好的查詢,甚至可以解釋如何正確使用連接。我對SQL很陌生,這是我非常有限的知識的原因。我之前已經閱讀過連接的說明,並且自己做了一個嘗試,但是看起來這個菜單對於我來說有點難以進行試驗和錯誤。

回答

2

儘管我討厭在WordPress中推薦直接SQL查詢(如果可以的話,您應該始終嘗試使用query_posts()),這可能是您的案例中唯一的選擇。也就是說,你將需要做兩個複雜的查詢。

首先,您需要運行查詢以獲取自定義菜單中的頁面。使用您的問題中列出的要求...

SELECT wtt.term_id AS term_id, wtt.taxonomy AS taxonomy, wt.name AS name, wtr.object_id AS object_id, wtr.term_taxonomy_id AS term_taxonomy_id, meta.post_id as post_id, meta.meta_key as meta_key, meta.meta_value AS meta_value, posts.post_title AS post_title, posts.post_status AS post_status, posts.guid AS guid, posts.post_parent AS post_parent, posts.post_type AS post_type 
    FROM wp_term_taxonomy AS wtt 
    INNER JOIN wp_terms AS wt ON wt.term_id = wtt.term_id 
    INNER JOIN wp_terms_relationships AS wtr ON wtr.term_taxonomy_id = wtt.term_id 
    INNER JOIN wp_postmeta AS meta ON meta.post_id = wtr.object_id 
    INNER JOIN wp_posts AS posts ON posts.id = meta.meta_value 
    WHERE wtt.taxonomy = "nav_menu" AND meta.meta_key = "_menu_item_object_id" 

這應該會給你一個與你說你需要適當的值的職位集合。然後,您需要循環運行集合並執行額外的查詢以獲取第二組數據中所需的信息。

///psuedocode 
for(...) { 

    SELECT posts.guid AS guid, posts.post_parent AS post_parent, posts.post_type AS post_type, meta.post_id AS post_id, meta.meta_key AS meta_key, meta.meta_value AS meta_value 
     FROM wp_posts AS posts 
     INNER JOIN wp_postmeta AS meta ON meta.post_id = posts.id 
     WHERE posts.post_parent = XXX AND posts.post_type = "attachment" AND meta.meta_key = "description" 

} 

在這種情況下,XXX是第一個查詢返回(通過在for(){ }循環迭代特定帖子的ID。

現實情況是,你的查詢很可能被簡化一個lot如果你可以更清楚地知道你實際需要什麼數據,由於第一個查詢只是爲了獲得菜單中的帖子列表,你可能不需要那麼大的SELECT聲明。這些值作爲你的問題中的「需要」。

+0

謝謝EAMann。我有一個更簡單的問題構建直接的SQL查詢,你的答案幫助我解決了這個問題。 – terraling 2014-01-10 20:13:49