• Resolved BakekaIncontritalia

    (@bakekaincontritalia)


    I am trying to sort my cutom 4 wordpress categories to display in the order that I want.

    I need help to complete the code:

    $categories = get_categories( $args );  
    
    usort($categories, function($a, $b, $c, $d) {
       return get_field("mycategorycustom1", "mycategorycustom1_".$a->term_id) - get_field("mycategorycustom2", "mycategorycustom2_".$b->term_id - get_field("mycategorycustom3", "mycategorycustom3_".$c->term_id - get_field("mycategorycustom4", "mycategorycustom4_".$d->term_id);
    });
    
    foreach ($categories as $category){
    
    /*??????????????????????????
    ???????????????
    ???????????
    ??????
    ?*/
    
    }
    • This topic was modified 7 years, 4 months ago by bcworkz. Reason: code fixed
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    usort() passes two array elements to your callback and it should return a integer indicating which of the two is greater than, less than or equal to the other. Do not collect four elements. usort() will eventually pass all elements, but only two at a time. I believe it uses a bubble sort algorithm, which only focuses on two elements at a time, repeatedly calling your function, swapping positions when appropriate. Eventually all the elements will “bubble” up to their correct position.

    The code you are asking for in foreach is simply to list the resulting array elements in some manner. What it is depends on what you want to display. To simply list the category names, just do esc_html_e( $category->name );

    Thread Starter BakekaIncontritalia

    (@bakekaincontritalia)

    Ok, thanks for the explanation

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sorting wordpress custom categories’ is closed to new replies.