I 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.
<?php // List posts by a Custom Field's values $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(); } } ?>
I'm still pretty basic at using wordpress custom posts myself, but I think the reason you're not seeing anything for the company is because you're storing the company metadata in the variable $rows2 = $wpdb->get_results($sql2); but you're not actually outputting anything from the $rows2 variable. You're only looping through $rows . I'm still learning php myself, but I'm sure there's an easier way to grab all that information too. I'll let some guru try and help you with that though.
Well, I had removed that part of it because it was not working. That was my attempt at ordering them by that field. I just copied the first section and tried to paste it into the first foreach loop.
If you do a var_dump() on $rows and $rows2, are you getting all the information that you want? If you could show me what that outputs I might be able to help you loop through it.
Also, here is the code that I am trying to get working.
<?php // List posts by a Custom Field's values $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 "<h3>$post->meta_value</h3>"; $current_value = $post->meta_value; if ($rows2) { foreach ($rows2 as $post2) { setup_postdata($post2); if ($post2->meta_value != $current_value2) { echo "<h3>$post2->meta_value</h3>"; $current_value2 = $post->meta_value; } the_title(); } } } } }
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.
$rows2 = $wpdb->get_results($sql2);but you're not actually outputting anything from the$rows2variable. You're only looping through$rows. I'm still learning php myself, but I'm sure there's an easier way to grab all that information too. I'll let some guru try and help you with that though.Let me know if that helps!
In case you've never used var_dump() before:
http://pastebin.com/GcRVK58N
http://pastebin.com/XkuGGztT
The data is exactly the same really. I just need to get that somehow formatted into that foreach statement.
Notice: Undefined variable: current_value in /var/www/test.drewclardy.com/public/wp-content/themes/drewclardy/content-beer-test.php on line 54