You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SportsPress插件:WPForms创建的比赛无法自动分配俱乐部求助

问题描述

在WordPress站点中使用SportsPress插件和WPForms工具搭建赛事系统,表单包含现有俱乐部复选框和1-2回合下拉菜单,提交后需自动创建循环联赛及对应比赛。目前提交表单后能生成“x vs y”标题的比赛,但比赛的俱乐部关联栏为空,手动在后台可以分配俱乐部。

现有代码(functions.php)

function create_sportspress_club_for_new_user($user_id) {
    // Get the user data
    $user_info = get_userdata($user_id);
    $username = $user_info->user_login;
    $profile_picture = get_user_meta($user_id, 'profile_photo', true);

    // Create a new club in SportsPress
    $club_id = wp_insert_post(array(
        'post_title' => $username,
        'post_type' => 'sp_team',
        'post_status' => 'publish',
    ));

    if (is_wp_error($club_id)) {
        return;
    }

    if ($club_id) {
        // Set the club profile picture
        if ($profile_picture) {
            $attachment_id = media_sideload_image($profile_picture, $club_id, null, 'id');
            if (!is_wp_error($attachment_id)) {
                set_post_thumbnail($club_id, $attachment_id);
            }
        }
        // Assign the club to the user
        update_user_meta($user_id, 'sp_team', $club_id);

        // Assign the user to the club in SportsPress settings
        update_post_meta($club_id, 'sp_team_manager', $user_id);

        // Ensure the user is added to the correct taxonomy terms if needed
        wp_set_object_terms($club_id, $user_id, 'sp_team_manager');

        // Add the new club to the sportspress_league_menu_teams option
        $teams = get_option('sportspress_league_menu_teams', array());
        if (!is_array($teams)) {
            $teams = array();
        }
        $teams[] = $club_id;
        update_option('sportspress_league_menu_teams', $teams);
    }
}

function create_matches_for_schedule($schedule) {
    foreach ($schedule as $match) {
        $home_club_id = intval($match[0]);
        $away_club_id = intval($match[1]);

        // Debugging: Log the IDs being used and their types
        error_log("Home Club ID: $home_club_id (Type: " . gettype($home_club_id) . "), Away Club ID: $away_club_id (Type: " . gettype($away_club_id) . ")");

        // Ensure the IDs are integers
        if (!is_int($home_club_id) || !is_int($away_club_id)) {
            error_log("Error: Club IDs are not integers. Home Club ID: $home_club_id, Away Club ID: $away_club_id");
            continue;
        }

        // Fetch team names using their IDs
        $home_club_name = get_the_title($home_club_id);
        $away_club_name = get_the_title($away_club_id);

        // Debugging: Ensure club names are correct
        error_log("Home Club Name: $home_club_name, Away Club Name: $away_club_name");

        // Check if the club names are valid
        if (empty($home_club_name) || empty($away_club_name)) {
            error_log("Error: Invalid club names. Home Club Name: $home_club_name, Away Club Name: $away_club_name");
            continue;
        }

        // Create a new match in SportsPress
        $match_id = wp_insert_post(array(
            'post_title' => "$home_club_name vs $away_club_name",
            'post_type' => 'sp_event',
            'post_status' => 'publish',
            'meta_input' => array(
                'sp_team_home' => $home_club_id,
                'sp_team_away' => $away_club_id,
            ),
        ));
    }
}

Debug日志内容

[24-Jan-2025 07:17:59 UTC] Selected clubs: Array
(
    [0] => mika
    [1] => Yoyo FC
)

[24-Jan-2025 07:17:59 UTC] Number of legs: 1
[24-Jan-2025 07:17:59 UTC] Club IDs: Array
(
    [0] => 1080
    [1] => 329
)

[24-Jan-2025 07:17:59 UTC] Generated schedule: Array
(
    [0] => Array
        (
            [0] => 1080
            [1] => 329
        )

)

[24-Jan-2025 07:17:59 UTC] Home Club ID: 1080 (Type: integer), Away Club ID: 329 (Type: integer)
[24-Jan-2025 07:17:59 UTC] Home Club Name: mika, Away Club Name: Yoyo FC

核心问题

表单提交后触发create_matches_for_schedule函数,生成的比赛标题正确,但比赛的俱乐部关联字段为空,如何修改函数实现自动分配选中的俱乐部?


解决方案

问题出在SportsPress存储赛事与球队关联的格式上:默认情况下,SportsPress的sp_team_home和sp_team_away元字段需要以数组格式存储,而非单个整数。同时拆分赛事创建与球队关联的步骤,能避免字段规范冲突。

修改后的create_matches_for_schedule函数

function create_matches_for_schedule($schedule) {
    foreach ($schedule as $match) {
        $home_club_id = intval($match[0]);
        $away_club_id = intval($match[1]);

        // Debugging: Log the IDs being used and their types
        error_log("Home Club ID: $home_club_id (Type: " . gettype($home_club_id) . "), Away Club ID: $away_club_id (Type: " . gettype($away_club_id) . ")");

        // 校验ID是否为有效SportsPress球队
        if (get_post_type($home_club_id) !== 'sp_team' || get_post_type($away_club_id) !== 'sp_team') {
            error_log("Error: Invalid team ID. Home: $home_club_id, Away: $away_club_id");
            continue;
        }

        // Fetch team names using their IDs
        $home_club_name = get_the_title($home_club_id);
        $away_club_name = get_the_title($away_club_id);

        // Debugging: Ensure club names are correct
        error_log("Home Club Name: $home_club_name, Away Club Name: $away_club_name");

        // Check if the club names are valid
        if (empty($home_club_name) || empty($away_club_name)) {
            error_log("Error: Invalid club names. Home Club Name: $home_club_name, Away Club Name: $away_club_name");
            continue;
        }

        // 创建赛事
        $match_id = wp_insert_post(array(
            'post_title' => "$home_club_name vs $away_club_name",
            'post_type' => 'sp_event',
            'post_status' => 'publish',
        ));

        if (!is_wp_error($match_id)) {
            // 以数组格式存储球队关联(符合SportsPress字段规范)
            update_post_meta($match_id, 'sp_team_home', array($home_club_id));
            update_post_meta($match_id, 'sp_team_away', array($away_club_id));
            
            // 可选:如需关联联赛,添加以下代码(替换为你的联赛ID)
            // wp_set_object_terms($match_id, $league_id, 'sp_league');
        }
    }
}

关键修改说明

  1. 字段格式调整:用update_post_meta单独设置球队关联,将单个ID包装为数组——SportsPress的球队字段为多值类型,必须以数组形式存储。
  2. 增加有效性校验:添加get_post_type检查,确保传入的ID是合法的SportsPress球队,避免无效数据导致关联失败。
  3. 拆分创建流程:先创建赛事主体,再设置关联字段,避免wp_insert_post的meta_input处理逻辑与SportsPress的字段规范冲突。

验证方式

修改代码后提交表单,检查以下内容:

  • 后台赛事详情的「球队」模块是否自动填充选中的俱乐部
  • 查看数据库wp_postmeta表,确认sp_team_home和sp_team_away的值为序列化数组格式(如a:1:{i:0;i:1080;})

内容的提问来源于stack exchange,提问作者Kirito

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 21:08:14