繁体   English   中英

加入5个表PHP SQL

[英]Join 5 tables PHP SQL

我有五个表来收集Wordpress项目中的高级自定义菜单的正确信息

这些是我需要的五个表和列

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很新,这是我知识非常有限的原因。 我之前已经阅读了连接的说明,并且自己尝试了,但是看起来这个菜单有点太难以为我做试验和错误。

尽管我讨厌在WordPress中推荐直接SQL查询query_posts()如果可以,你应该总是尝试使用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是第一个查询返回的特定帖子的ID(在for(){ }循环中迭代。

实际情况是,如果您可以更清楚地了解实际需要的数据,那么您的查询可能会被简化很多 由于第一个查询只是为了获取菜单中的帖子列表,因此您可能不需要那么大的SELECT语句。 我之所以加入它,是因为您在问题中将每个值都称为“需求”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM