Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Ok it is pretty simple. Every post of my blog has a specific country. So i have the custom field country and then, when i write the article i add: japan, argentina, usa, italy etc. to the post. Now in home i like to have a box like that:

Post By Country:

Japan (130 posts) Usa (104 posts) Argentina (90 posts) Italy (46 posts) Uk (23 post)

etc, etc. Then, be able to click on a contry and have all the post from that country. I'm not so familiar with php queries, can u help me?

Tnx a lot.

share|improve this question
2  
Giorgio, If you want to ask for clarification on an answer you should post a comment on the answer. Please read through the FAQ to familiarize yourself with the way this site works: wordpress.stackexchange.com/faq As far as how to have a box in the sidebar that querys the custom fields and then posts links to custom loops, I am sure there is a way to do this but that is beyond my skill set. I know for a fact that it can be done much easier with custom taxonomies however I have never done this and don't know the exact code for you to use. Maybe someone else will be more help than I was. –  matt Dec 9 '10 at 19:53
add comment

2 Answers

Instead of using a custom field, you should add a custom taxonomy. You can query posts by taxonomy then.

share|improve this answer
add comment

Here's a loop that pulls all your post with the custom field key "Country"...

<?php
$countryPosts = new WP_Query();
$countryPosts->query('meta_key=country');
?>
<?php while ($countryPosts->have_posts()) : $countryPosts->the_post(); ?>
CONTENT STUFF HERE
<?php endwhile; ?>

Now to order that you can add order=ASC or order=DESC so this line reads:

$countryPosts->query('meta_key=country&order=ASC');

Here's the loop that will show all the post that have the custom field filled out for a specific country, in this example, Japan:

<?php
$countryPosts = new WP_Query();
$countryPosts->query('meta_key=country&meta_value=japan');
?>
<?php while ($countryPosts->have_posts()) : $countryPosts->the_post(); ?>
CONTENT STUFF HERE
<?php endwhile; ?>

Update: After re-reading your question I realized I may not fully understand what you are trying to do correctly. The above code will let you display posts from a specific country but it won't show that "archives" type navigation box. From the sound of it, you may want to create a custom taxonomy for countries rather than using a custom field. This will make it a lot easier to interact with the posts based on the Country.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.