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.

wp_get_post_terms Order by not working

My code is here

query_posts(array('post_type' => 'product','post__in' => $product_ids,));

while (have_posts()) : the_post();
    global $post;

$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
$product_terms = wp_get_object_terms($post->ID, 'productcategory',$args);
if(!empty($product_terms)){
  if(!is_wp_error( $product_terms )){
    echo '<ul>';
    foreach($product_terms as $term){
      echo '<li><a href="'.get_term_link($term->slug, 'product').'">'.$term->name.'</a></li>'; 
    }
    echo '</ul>';
  }
}

endwhile;

I read read on the documentation that its support array $args to make order the terms by "name", ASC however I don't know why it wont work

share|improve this question

1 Answer 1

Your title is

wp_get_post_terms Order by not working

but if you use a custom post type you need to use wp_get_object_terms()

I'm not sure what your aim is, but here is a code example you can try:

$my_post_type="post"; // edit this 
$my_taxonomy="category"; // edit this

$my_query = new WP_Query(array('post_type' => $my_post_type,'posts_per_page'=>10));

if ($my_query->have_posts()) : 
    while ($my_query->have_posts()) : $my_query->the_post(); 
        $product_terms = wp_get_object_terms(get_the_ID(), $my_taxonomy, array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all'));
        if(!empty($product_terms)){
            if(!is_wp_error( $product_terms )){
                echo '<ul>';
                foreach($product_terms as $term){
                    echo '<li><a href="'.get_term_link($term->slug, $my_taxonomy).'">'.$term->name.'</a></li>'; 
                }
            echo '</ul><hr>';
        }
    }
    endwhile;
endif;

ps: I changed your code a bit, so now we use WP_Query() instead of the non-recommended query_posts().

Edit:

Here is an example of the output, where the terms are ordered with name and ASC

aaa
bbb
ccc
---
aaa
ccc
---
bbb
ccc

and with order DESC

ccc
bbb
aaa
---
ccc
aaa
---
ccc
bbb
share|improve this answer
    
not working , what I wanted to achieve is to make product term order by name ASC –  idontknowhow Feb 16 '13 at 18:54
    
I updated the code so you can test it with custom post type post and taxonomy category to debug it. This code example works on my install. I changed DESC to ASC for the terms order. What is your taxonomy name and custom post type name (product?) –  birgire Feb 16 '13 at 19:17
    
It doesn't work either, I have custome post type product and taxonomy product term, what I want is to order post product taxonomy in ASC order –  idontknowhow Feb 17 '13 at 12:14
    
Do you get any output or is it empty? You could try var_dump($my_query); to see if the query is also empty. Do you have a space in your taxonomy name: product term or is it also product? –  birgire Feb 17 '13 at 12:26
    
nope I get output but unorderd, I get all product categories that selected on each post but each post dont have ordered productegory they selected, I dont even have space on my product term or product –  idontknowhow Feb 17 '13 at 12:56

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.