我有两个CPT——Match
和Player
. 每个Match
post entry包含与特定比赛相关的详细信息(谁比赛、得分手等)。该信息通过与个人对应的ACF Post对象字段输入Player
. 不过,我希望实现的是在每个Player
发布总共有多少次出场、次出场和进球。
为此,我使用以下方法meta_query
获取球员开始比赛的总成绩。查询很繁琐,但它可以完成这项工作。不过,我现在遇到的问题是,试图使用相同的查询结构生成子外观的总数会耗尽内存,并返回错误500。
$args = array(\'post_type\' => \'match\',
\'meta_query\' => array(\'relation\' => \'OR\',
array(\'relation\' => \'AND\',
array(\'key\'=> \'pl12\',
\'value\'=> $playerID,
\'compare\'=> \'=\',),
array(\'key\'=> \'si12\',
\'value\'=> \'0\',
\'compare\'=> \'!=\',),
),
array(\'relation\' => \'AND\',
array(\'key\'=> \'pl13\',
\'value\'=> $playerID,
\'compare\'=> \'=\',),
array(\'key\'=> \'si13\',
\'value\'=> \'0\',
\'compare\'=> \'!=\',),
),
...
array(\'relation\' => \'AND\',
array(\'key\'=> \'pl18\',
\'value\'=> $playerID,
\'compare\'=> \'=\',),
array(\'key\'=> \'si18\',
\'value\'=> \'0\',
\'compare\'=> \'!=\',),
),
),
);
$subs = new WP_Query( $args );
echo "(".$subs->found_posts.")";
wp_reset_postdata();
UPDATE: SOLUTION
使用
functions.php
正如所建议的那样,自定义SQL查询提供了可靠的工作解决方案,可以为每个查询提供总外观、子外观和目标的输出
Player
使用
Match
发布数据。
function get_player_stats( $player_id ) {
global $wpdb;
$stats = [];
$stats[\'apps\'] = (int) $wpdb->get_var( $wpdb->prepare( "
SELECT COUNT(pm.meta_id) FROM {$wpdb->postmeta} pm
INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key IN (\'PL1\',\'PL2\',\'PL3\',\'PL4\',\'PL5\',\'PL6\',\'PL7\',\'PL8\',\'PL9\',\'PL10\',\'PL11\')
AND pm.meta_value = %d
AND p.post_type = \'match\'
AND p.post_status = \'publish\'
", $player_id ) );
$stats[\'subs\'] = (int) $wpdb->get_var( $wpdb->prepare( "
SELECT COUNT(DISTINCT pm.meta_id) FROM {$wpdb->postmeta} pm
INNER JOIN {$wpdb->postmeta} pm2 ON pm2.post_id = pm.post_id
INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE 1
AND (
pm.meta_key IN (\'PL12\', \'PL13\', \'PL14\', \'PL15\', \'PL16\', \'PL17\', \'PL18\')
AND pm.meta_value = %d
)
AND (
pm2.meta_key IN (\'si12\',\'si13\',\'si14\',\'si15\',\'si16\',\'si17\',\'si18\')
AND ( SUBSTR(pm2.meta_key, 3) = SUBSTR(pm.meta_key, 3) )
AND pm2.meta_value+0 > 0
)
AND p.post_type = \'match\'
AND p.post_status = \'publish\'
", $player_id ) );
$stats[\'goals\'] = (int) $wpdb->get_var( $wpdb->prepare( "
SELECT COUNT(pm.meta_id) FROM {$wpdb->postmeta} pm
INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key REGEXP \'^S[0-9]+$\'
AND pm.meta_value = %d
AND p.post_type = \'match\'
AND p.post_status = \'publish\'
", $player_id ) );
return $stats;
}
最合适的回答,由SO网友:Sally CJ 整理而成
在我之前的回答中(我已经删除了…),我建议使用自定义SQL查询,这对我很有用。但是,由于它对您不起作用(很好),让我们尝试使用此函数:
function get_player_stats( $player_id ) {
$apps = 0; // Total appearances.
$subs = 0; // Total sub-appearances.
$goals = 0; // Total goals.
// Get all published `match` posts.
$posts = get_posts( [
\'post_type\' => \'match\',
\'posts_per_page\' => -1,
] );
foreach ( $posts as $post ) {
for ( $n = 1; $n <= 18; $n++ ) {
// Check if the player is $player_id
$pl = get_post_meta( $post->ID, \'pl\' . $n, true );
if ( $player_id == $pl ) {
$apps++;
// Check his sub-out appearance.
$so = get_post_meta( $post->ID, \'so\' . $n, true );
$subs += $so ? 1 : 0;
// Check his sub-in appearance.
$si = get_post_meta( $post->ID, \'si\' . $n, true );
$subs += $si ? 1 : 0;
}
// Check if he scored goal {$n}-th.
$s = get_post_meta( $post->ID, \'s\' . $n, true );
if ( $player_id == $s ) {
$goals++;
}
}
}
// WP_Query was called; so resets $wp_query
wp_reset_query();
return compact( \'apps\', \'subs\', \'goals\' );
}
如果您有超过18名球员(在任何比赛中),请更改
18
在
$n <= 18;
达到正确的玩家数量;e、 g。
20
如中所示
$n <= 20;
.
下面是您如何使用get_player_stats()
函数返回一个关联数组,其中apps
, subs
, 和goals
作为键:
$stats = get_player_stats( $playerID );
//var_dump( $stats );
echo $stats[\'apps\'] . \' apps, \' .
$stats[\'subs\'] . \' subs, and \' .
$stats[\'goals\'] . \' goals.<br>\';
我希望这个答案对您有用,如果您需要任何澄清(关于代码的任何部分),请告诉我。=)