Join 5 tables PHP SQL
I have five tables which is needed to recieve the correct information to a advanced custom menu in a Wordpress project
These are the five tables and the columns I need
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"
I hope this makes just a little sense.
What i'm trying to do is basicly, to create a drop down menu that contains the list of pages i开发者_运维知识库n the WordPress custom menu feature, take each page's featured image, and their Custom field Description where I have a little text to display.
The final menu looks like this with styling:
So far I have had a succes making the menu work, but not with a very nice type of code:
<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>
Hopefully, some of you can help me achieve the same result, but with a better query and maybe even explain me how to use joins properly. I'm quite new to SQL, which is the reason for my very limited knowledge. I have prior to this read the instructions of joins, and made an attempt myself, but it just seems like this menu is a bit too hard to do trial and error on for me.
As much as I hate to recommend direct SQL queries in WordPress (you should always try to use query_posts()
if you can), that might be the only option in your case. That said, you're going to need to do two complicated queries.
First, you need to run the query to get the pages in the custom menu. Using the requirements listed in your question ...
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"
This should give you a collection of posts with the appropriate values that you say you need. Then you'll need to run through the collection in a loop and performs an additional query to get the information you wanted in your second set of data.
///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"
}
In this case, the XXX
is the ID of a specific post returned by the first query (as iterated through in your for(){ }
loop.
The reality is that your queries could probably be simplified a lot if you can be more clear regarding what data you actually need. Since the first query is just to get a list of posts in the menu, you probably don't need that huge SELECT
statement. I only included it because you stated each of those values as "needs" in your question.
精彩评论