- This topic is empty.
-
AuthorPosts
-
July 24, 2012 at 12:36 pm #39034
dclardy
MemberI have been working on my site, and I have added a new beer section to it. It is currently working here, http://drewclardy.com/beers, but I would like to make some changes to that styling.
I would like to order it in this format.
[Country]
[Company]
[Beer]I am not super advanced at PHP or WordPress, but I was able to get the current setup with working with WP_Query. Does anyone have any ideas on how to get this to work. I am trying this code, and I can get it to list the Country and then the beer. It does not list the Company though. I think I am doing something wrong with the looping.
$meta_key = 'brewing_country'; // The meta_key of the Custom Field
$sql = "
SELECT p.*,m.meta_value
FROM $wpdb->posts p
LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)
WHERE p.post_type = 'beer'
AND p.post_status = 'publish'
AND m.meta_key = '$meta_key'
ORDER BY m.meta_value, p.post_date DESC
";
$meta_key2 = 'brewing_company'; // The meta_key of the Custom Field
$sql2 = "
SELECT p.*,m.meta_value
FROM $wpdb->posts p
LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)
WHERE p.post_type = 'beer'
AND p.post_status = 'publish'
AND m.meta_key = '$meta_key2'
ORDER BY m.meta_value, p.post_date DESC
";
$rows = $wpdb->get_results($sql);
$rows2 = $wpdb->get_results($sql2);
if ($rows) {
foreach ($rows as $post) {
setup_postdata($post);
if ($post->meta_value != $current_value) {
echo "$post->meta_value";
$current_value = $post->meta_value;
}
the_title();
}
}
?>July 24, 2012 at 1:12 pm #106692dclardy
MemberAlso, here is the code that I am trying to get working.
$meta_key = 'brewing_country'; // The meta_key of the Custom Field
$sql = "
SELECT p.*,m.meta_value
FROM $wpdb->posts p
LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)
WHERE p.post_type = 'beer'
AND p.post_status = 'publish'
AND m.meta_key = '$meta_key'
ORDER BY m.meta_value, p.post_date DESC
";
$meta_key2 = 'brewing_company'; // The meta_key of the Custom Field
$sql2 = "
SELECT p.*,m.meta_value
FROM $wpdb->posts p
LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)
WHERE p.post_type = 'beer'
AND p.post_status = 'publish'
AND m.meta_key = '$meta_key2'
ORDER BY m.meta_value, p.post_date DESC
";
$rows = $wpdb->get_results($sql);
$rows2 = $wpdb->get_results($sql2);
if ($rows) {
foreach ($rows as $post) {
setup_postdata($post);
if ($post->meta_value != $current_value) {
echo "$post->meta_value
";
$current_value = $post->meta_value;
if ($rows2) {
foreach ($rows2 as $post2) {
setup_postdata($post2);
if ($post2->meta_value != $current_value2) {
echo "$post2->meta_value
";
$current_value2 = $post->meta_value;
}
the_title();
}
}
}
}
}
?>July 24, 2012 at 1:13 pm #106693dclardy
MemberThis produces an error on the page.
Notice: Undefined variable: current_value in /var/www/test.drewclardy.com/public/wp-content/themes/drewclardy/content-beer-test.php on line 54
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.