WordPress.org

Support

Support » How-To and Troubleshooting » SELECT SUM of meta_value, with multiple categories

SELECT SUM of meta_value, with multiple categories

  • Hello,

    with this query, i count the total of the sum of the meta_value of “adherents” meta_key, on the posts of category with ID 1:

    <?php
    $test_count = $wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->posts
    LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
    LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->term_taxonomy.term_id = 1
    AND $wpdb->term_taxonomy.taxonomy = 'category'
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->postmeta.meta_key = 'adherents'
    ORDER BY $wpdb->postmeta.meta_value ASC");
    echo '' . $test_count . '';
    ?>

    so, i need to check posts of multiple categories, like that:

    WHERE $wpdb->term_taxonomy.term_id = 1,2,3

    but this doesn’t work..

    $wpdb->term_taxonomy.term_id IN (1,2,3)

    doesn’t work too…

    what is the problem?

    bye,
    fred

  • The topic ‘SELECT SUM of meta_value, with multiple categories’ is closed to new replies.