我有一个带有字段的自定义帖子类型Exercise
和Tempo
. 下面是一个可能的示例:
练习1-100
练习2-104
练习3-96
练习1-104
练习2-110
练习3-100
如何使用WP\\u Query仅返回最高Tempo
各的Exercise
?
期望的结果是:
练习1-104练习2-110练习3-100
编辑:
我终于按照Rarst的建议让它工作了(见下文)。谢谢你的帮助!
function opl_user_practice_sheet_test($atts, $content = null){
if ( is_user_logged_in()) {
global $current_user;
wp_get_current_user();
echo \'User: \' . $current_user->user_login . "\\n";
global $post;
$atts = shortcode_atts( array(
\'title\' => \'User Practice Sheet\',
\'count\' => -1
), $atts);
// query arguments
$args = array(
\'post_type\' => \'opl_exercise\',
\'post_status\' => \'publish\',
\'orderby\' => \'created\',
\'order\' => \'DESC\',
\'posts_per_page\' => $atts[\'count\']
);
// fetch practice sheet tempos
$practice_sheet_exercises = new WP_Query($args);
// check if tempos come back
if($practice_sheet_exercises->have_posts()){
// init output
$output = \'\';
// build output
$output .= \'<div class="user-practice-sheet">
<table>
<thead>
<tr>
<td>Exercise</td>
<td>Submitted Tempo</td>
<td>Next Target Tempo</td>
</tr>
</thead>
<tbody>\';
while($practice_sheet_exercises->have_posts()){
$practice_sheet_exercises->the_post();
$exercise_type = get_post_meta($post->ID, \'opl_exercise_type\', true);
$exercise_number = get_post_meta($post->ID, \'opl_exercise_number\', true);
$tempo_query = new WP_Query(
array(
\'post_type\' => \'opl_tempo_submission\',
\'order_by\' => \'meta_value_num\',
\'meta_key\' => \'opl_submission_tempo\',
\'order\' => \'DESC\',
\'posts_per_page\' => 1,
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'opl_submission_exercise_type\',
\'value\' => $exercise_type,
\'compare\' => \'=\'
),
array(
\'key\' => \'opl_submission_exercise_number\',
\'value\' => $exercise_number,
\'compare\' => \'=\'
)
)
)
);
if ( $tempo_query->have_posts() ) {
while ( $tempo_query->have_posts() ) {
$tempo_query->the_post();
$submission_tempo = get_post_meta($post->ID, \'opl_submission_tempo\', true);
$output .= \'<tr>\';
$output .= \'<td>\'.$exercise_type .\' \'. $exercise_number.\'</td>\';
$output .= \'<td>\'.$submission_tempo.\'</td>\';
$output .= \'<td></td>\';
$output .= \'</tr>\';
}
}
}
$output .= \'</tbody>
</table>
</div>\';
// reset post data
wp_reset_postdata();
return $output;
} else {
return \'<p>No Practice Sheet Found</p>\';
}
}
}