简体   繁体   English

汇总特定类别中所有帖子的元值

[英]SUM the meta value of all posts that are in a specific category

I have a Nexus 4 category with a slug of: lg-nexus-4-e960 我有一个类别为Nexus 4的类别:lg-nexus-4-e960

In this category, I have 100 posts. 在这个类别中,我有100个帖子。 In the wp_postmeta table, each of these posts has a meta_field = sales_count, and a meta_value = 1, or more. 在wp_postmeta表中,每个帖子都有meta_field = sales_count和meta_value = 1或更大。

What I would like is to echo, outside the loop on a custom template, the total value of all the sales_count combined for that category. 我想在自定义模板的循环之外回显该类别组合的所有sales_count的总价值。 Essentially getting the amount of sales. 从根本上获得销售量。

I have some idea of how it might work, but i do not have the knowledge to implement it. 我对它可能如何工作有一些想法,但是我不知道如何实现它。 Query the wp_postmeta table, where the custom fields are being saved, and use the sum() sql function? 查询wp_postmeta表,其中保存了自定义字段,并使用sum()sql函数?

I tried this code, but it lacks a category specification: 我尝试了这段代码,但是缺少类别规范:

<?php
$meta_key = 'sales_count';//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", $meta_key));
echo '<p>Total miles is '.$allmiles . '</p>';
?>

Maybe not the most elegant solution 也许不是最优雅的解决方案

<?php

$catPost = get_posts(array('category' => get_cat_id("yourCategory"))); //change this
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 = 'sales_count';//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>';
?>

a faster way: 更快的方法:

$allmiles = $wpdb->get_var($wpdb->prepare( 
            "SELECT sum( meta_value )
            FROM $wpdb->postmeta
            WHERE meta_key LIKE %s
            AND post_id
            IN ( 
            SELECT post_id
            FROM $wpdb->term_relationships
            WHERE term_taxonomy_id = %d
            )" , $meta_key , $cat_id ) );

For anyone still looking for more detailed solution, I have the following working based on the above answer by chiliNUT - Conditional based on page ID: 对于仍在寻找更详细解决方案的任何人,我根据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 } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM