0

Wordpress プロジェクトの高度なカスタム メニューに正しい情報を受け取るために必要な 5 つのテーブルがあります。

これらは私が必要とする5つのテーブルと列です

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 にまったく慣れていないため、知識が非常に限られています。私はこれより前に結合の指示を読み、自分で試してみましたが、このメニューは試行錯誤するには少し難しすぎるようです.

4

1 に答える 1

2

WordPress で直接 SQL クエリを使用することをお勧めしたくありませんが (query_posts()可能な場合は常に使用するようにしてください)、それが唯一の選択肢である可能性があります。つまり、2 つの複雑なクエリを実行する必要があります。

まず、クエリを実行してカスタム メニューのページを取得する必要があります。質問に記載されている要件を使用して...

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"

これにより、必要な適切な値を持つ投稿のコレクションが得られるはずです。次に、コレクションをループで実行し、追加のクエリを実行して、2 番目のデータ セットで必要な情報を取得する必要があります。

///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は最初のクエリによって返された特定の投稿の ID です (for(){ }ループ内で繰り返されます)。

現実には、実際に必要なデータをより明確にすることができれば、クエリを大幅に簡素化できる可能性があります。SELECT最初のクエリは、メニュー内の投稿のリストを取得するだけなので、おそらくそのような巨大なステートメントは必要ありません。質問でこれらの値のそれぞれを「ニーズ」として述べたので、私はそれを含めただけです.

于 2011-03-18T14:27:30.200 に答える