Custom Fields
Re-Envisioned
Support Meta Box Group Sort sub field based on sub field value

This topic contains 3 replies, has 2 voices, and was last updated by  Anh Tran 1 year, 6 months ago.

  • Creator
    Topic
  • #8442
    Resolved
    alisha
    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #8451

    Anh 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

    alisha
    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 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 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.