Sort sub field based on sub field value

Support MB Group Sort sub field based on sub field valueResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #8442
    alishaalisha
    Participant

    Hello,
    Maybe this is my last question.Well, I am trying to building a football league point table.I have used following codes to create the metabox.

    https://pastebin.com/YUWXpjuj

    https://prnt.sc/i9n06o

    And HTML/PHP codes below.

    https://pastebin.com/9K9WrvLB

    HTML/PHP output is below.

    https://prnt.sc/i9n0ni

    Now my query is:
    Is it possible to show the point table on the front side based on the sub group field as for example number of match win (id:football_team_win_stat). More number of win team will be first and so on. Of course I can build the table from back end through drag and drop option.

    #8451
    Anh TranAnh Tran
    Keymaster

    Yes, you can. After getting group value, you can sort sub-groups using a custom function, like this:

    $group = get_post_meta( get_the_ID(), 'football_league_table_option', true );
    usort( $group, function ( $a, $b ) {
        $score_a = $a['football_team_win_stat'];
        $score_b = $b['football_team_win_stat'];
        if ( $score_a == $score_b ) {
            return 0;
        }
        return ( $score_a > $score_b ) ? - 1 : 1;
    } );
    $counter = 0;
    // other code
    #8464
    alishaalisha
    Participant

    It shows following warning.
    Warning: usort() expects parameter 1 to be array, string given in C:\wamp\www\wordpress\wp-content\themes\goalkick\loop\popular-post.php on line 157

    #8481
    Anh TranAnh Tran
    Keymaster

    Please check the field ID in the code. I didn't know which $prefix you used, so I use football_. Not sure if that's right.

    Anyway, do not just copy and paste the code 😉

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.