0

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.

1

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.

  • 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
1

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)
                    )
            ));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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