• Resolved mcseay

    (@mcseay)


    How can I sum the values (they are all numeric) of a custom field filtered by the taxonomy of a post type?
    For example, I want to sum all the values of the custom field ‘wpcf-litter-puppies’ of the post type ‘puppies’ but only in the taxonomy ‘litter-type’ of ‘previous-litters’.
    So…
    ‘post_type’ => ‘puppies’,
    ‘taxonomy’ => ‘litter-type’,
    ‘term’ => ‘previous-litters’,
    ‘custom_field’ => ‘wpcf-litter-puppies’

Viewing 1 replies (of 1 total)
  • Thread Starter mcseay

    (@mcseay)

    Found a solution (probably not the best way to do this)…

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. ]

    <!-- Start Counting Loop -->
    <?php
    	$args = array(
    		'post_type' => 'puppies',
    		'taxonomy' => 'litter-type',
    		'term' => 'previous-litters',
    		'posts_per_page' =>-1
    	);
    	$loop = new WP_Query($args);
    	if ( $loop->have_posts() ) : ?>
        <?php
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
    		<?php $amt = get_post_meta($post->ID, 'wpcf-litter-puppies', true);?>
    		<?php if ($amt) {$previous_puppies_total += $amt;};?>
    		echo $total; ?>
        <?php endwhile; ?>
    	<?php echo 'Puppies from previous litters: '.$previous_puppies_total; ?>
    <?php endif; ?>
    <?php wp_reset_postdata(); ?>
    <!-- End Counting Loop -->
Viewing 1 replies (of 1 total)
  • The topic ‘Sum values of custom fields filtered by custom post type and taxonomy’ is closed to new replies.