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.

My Query

$my_query = array(
                'posts_per_page' => 10,
                'tax_query' => array(
                        array(
                            'taxonomy' => 'country',
                            'field' => 'id',                            
                            'terms' => array(4)
                        )
                ));

Added Custom Taxonomy Using

function taxonomy_init() {
    // create a new taxonomy

    register_taxonomy(
        'country',
        'post',
            array(
                'label' => __('Countries (Labels)'),
                'sort' => true,
                'args' => array('orderby' => 'term_order'),
                'rewrite' => array('slug' => 'location')
            )
    );


}
add_action( 'init', 'taxonomy_init' );

Whats wrong with it? I have 2 posts using that taxonomy.

share|improve this question

2 Answers 2

you mixed the query with the register function, try this:

function taxonomy_init() {
    // create a new taxonomy
    register_taxonomy(
        'country',
        'post',
            array(
                'label' => __('Countries (Labels)'),
                'rewrite' => array('slug' => 'location')
            )
    );
}
add_action( 'init', 'taxonomy_init' );

the in your query use

$my_query = array(
                'posts_per_page' => 10,
                'tax_query' => array(
                        array(
                            'taxonomy' => 'country',
                            'field' => 'id',                            
                            'terms' => array(4)
                        )
                ));

since you are querying only one term you don't need to order by term_id.

if you are trying to achive something else the please explain a bit more.

share|improve this answer
    
Im using posts as products, so here I want to sort them down by country. So I just want to show a list of countries like categories, but right now it shows something like tags. I am using $my_query... in a different function and no inside the register_taxonomy function. I'll try this code and update you. –  Atif Mohammed Ameenuddin Mar 1 '11 at 6:42
    
this doesn't work too –  Atif Mohammed Ameenuddin Mar 1 '11 at 17:54
    
this will only get you products in country term_id 4, if you wan to sort, by country then that's a different query. let me know if that is what you want i'll post a new query –  Bainternet Mar 1 '11 at 18:14
    
no I don't want to sort it, sorry in my earlier message i meant to filter them. I want to get items of term_id 4 but its not getting any. –  Atif Mohammed Ameenuddin Mar 3 '11 at 12:39

You need to mention the post_type argument.

    $my_query = array(
            'post_type' => 'custom_post_type',
            'posts_per_page' => 10,
            'tax_query' => array(
                    array(
                        'taxonomy' => 'country',
                        'field' => 'id',                            
                        'terms' => array(4)
                    )
            ));
share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.