对于仍在寻找更详细解决方案的任何人,我根据 chiliNUT 的上述答案进行了以下工作 - 基于页面 ID 的条件:
<?php if ( is_page( '123' ) ) { ?>/* change number */
<?php } elseif ( is_page( '321' ) ) { ?> /* change to different number */
<?php
$catPost = get_posts(array('post_type' => 'your_post_type', 'post_status' => array('publish', 'future'),'tax_query' => array(array('taxonomy' => 'your_cpt_categories', 'field' => 'slug', 'terms' => 'your-category-slug')))); //change these to suit your registered custom post type, taxonomies, post status, etc.
foreach ($catPost as $post) {
setup_postdata($post);
$ids[] = get_the_ID();
}
//we now have an array of ids from that category, so then
$idList = implode(",", $ids); //turn the array into a comma delimited list
$meta_key = 'your_meta_key';//set this to your custom field meta key
$allmiles = $wpdb->get_var($wpdb->prepare("
SELECT sum(meta_value)
FROM $wpdb->postmeta
WHERE meta_key = %s
AND post_id in (" . $idList . ")", $meta_key));
echo '<p>Total miles is ' . $allmiles . '</p>';
?>
<?php } else { ?>
<?php the_content(); ?> /* or do other stuff */
<?php } ?>