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

  • Creator
  • #8442
    Resolved alisha

    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.

    And HTML/PHP codes below.

    HTML/PHP output is below.

    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
  • #8451
    Anh Tran

    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

    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

    Anh Tran

    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.