0

I need to sort and paginate custom taxonomy post types. i have a page where i have next and previous link. on clicking next and previous link next or previous brand(custom taxonomy) should load. I have achieved this so far

 $terms = get_the_terms( get_the_ID(), 'brand' );
if($terms) {
  $BrandId=$terms[0]->term_id;
  $nextBrandId=$BrandId+1;
  $prevBranId=$BrandId-1;
  $next=get_term_by('id', $nextBrandId, 'brand' );
  $prev=get_term_by('id', $prevBranId, 'brand' );

  //echo '<pre>';
   echo $next->slug;
    echo $prev->slug;
   // echo '</pre>';
 }

I am getting next and previous brand slug by id and they are showing randomly (if i talk in alphabetic order).

Please tell me how can i show next and previous link that are sorted by alphateicallhy.

For eg I have following brands

Versace, Valentino, Armani, Rado, CK, Tissot

If currently i am at Tissot and click next then i should go to Valentino. If i am at Tissot and click prev then i should go to Rado.

2
  • You either need to use usort or wp_get_post_terms to sort your post terms by name Commented May 6, 2015 at 7:14
  • can you please share some example. Commented May 6, 2015 at 7:23

2 Answers 2

1

As I stated in comments, you can either use usort() to sort the returned array of terms, or you can just use wp_get_post_terms() which is already sorted by default by name in ascending order

$terms = wp_get_post_terms( get_the_ID(), 'brand' );
var_dump( $terms );

EDIT

Here is an example with usort() and get_the_terms()

$terms = get_the_terms( get_the_ID(), 'brand' );
usort( $terms, function ( $a, $b )
{

    return strcasecmp( $a->name, $b->name );

});
6
  • see i know that i need to use usort() but i am not understanding and getting the point that how should i use this. Because i am in a situation. where i have current Active brand id name and details and want to move to the very next or previous brand in alphabetic orders. I just need some code example. Commented May 6, 2015 at 10:01
  • You don't have to use usort(), you can use wp_get_post_terms() which is already sorted by name. It does the same exact thing as get_the_terms. I will update my answer to include usort() Commented May 6, 2015 at 10:09
  • see i have following scenario awesomescreenshot.com/image/186364/… i am having listing of products inside custom taxonomy in a page and want to move to next taxonomy page where taxonomy products will be displayed. i cannot use wp_get_post_terms and if i can then how will i get the slug of next or previous brand. Commented May 6, 2015 at 10:16
  • See my update. Hope that is what you are looking for Commented May 6, 2015 at 10:24
  • I think usort() with strcasecmp() would be a bad idea, since strcasecmp() is not utf8 save as far as I know. See for example here: stackoverflow.com/questions/14655092/comparing-utf-8-string Commented May 6, 2015 at 10:29
0

I was trying the same thing to achieve this but finally i found a better solution here. It works with regular taxonomies (categories) and also work with custom taxonomies. You should check its code inside or can use it as it is if the requirements are the same.

2
  • Welcome to the community. Two suggestions: If the external link goes down or changes, then we might not be any wiser regarding the solution, so please add the relevant information into the answer. If you're promoting your own plugin, it would be nice to know that too. Thanks.
    – birgire
    Commented May 7, 2015 at 8:47
  • I could paste that code snippet here but that will not make any sense except that a query. WordPress custom post taxonomies are involved and a few hooks are used in that, it would be a lengthy answer in that way and still will not help as compare to a working/ready code. Commented May 8, 2015 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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