Support

Account

Home Forums ACF PRO Custom post query with multiple fields with number values. Totalisation needed

Solving

Custom post query with multiple fields with number values. Totalisation needed

  • The following code gives me several tables (sorted by category) with custom fields and number values. What I need now, is a totalisation at the end of every table. How can I do that? Thanks!

    <div class="togglecontent"> <?php
    $portraetrangliste = get_the_id();
    
      $catargs = array(
             'taxonomy'     => 'jahr',
            'order' => 'DESC',
      );
    $cats = get_categories($catargs);
    
    foreach ($cats as $cat) {
    $args = array(
            'posts_per_page' => -1,
        'post_type'     => 'rangliste',
        'meta_key'      => 'portrait_nid',
            'order' => 'DESC',
            'orderby' => 'date',
            'post_status' => 'publish',
        'meta_value'    =>  $portraetrangliste,
    'tax_query' => array(
        array(
            'taxonomy' => 'jahr',
            'field'    => 'term_id',
            'terms'    => $cat->cat_ID,
            ),
        ),
    );
    global $post;
    $query = new WP_Query($args);
    if ( $query->have_posts() ): ?> <h5>Saison <?php echo $cat->cat_name ; ?> </h5>
      <table class="portraetrangliste">
        <tr>
          <th>Rang</th>
          <th>Kranz</th>
          <th>Sieger</th>
          <th>Jahreswertung<br />Punkte</th>
          <th>Bezwungene <br /> Eidgenossen </th>
          <th>Datum / Fest</th>
        </tr> <?php
    
       while($query -> have_posts()) : $query -> the_post(); ?> <tr>
          <td>
            <strong> <?php the_field( 'rang' ); ?> </strong>
          </td>
          <td> <?php if ( get_field( 'kranzgewinn' ) == 1 ) : ?> <?php echo 'Ja'; ?> <?php else : ?> <?php echo 'Nein'; ?> <?php endif; ?> </td>
          <td> <?php if ( get_field( 'sieger' ) == 1 ) : ?> <?php echo 'Ja'; ?> <?php else : ?> <?php echo 'Nein'; ?> <?php endif; ?> </td>
          <td> <?php the_field( 'punkte_jahresliste' ); ?> </td>
          <td> <?php the_field( 'bezwungene_eidgenossen' ); ?> </td>
          <td> <?php global $post; ?> <?php $fest_nid = get_field( 'fest_nid' ); ?> <?php if ( $fest_nid) : ?> <?php $post = $fest_nid; ?> <?php setup_postdata( $post ); ?> <?php echo get_the_date('d.m.Y'); ?> - <a href="
                        
                        
                        <?php the_permalink(); ?>"> <?php the_title(); ?> </a> <?php wp_reset_postdata(); ?> <?php endif; ?> </td>
        </tr> <?php
       endwhile; ?>
      </table> 
     <?php endif; 
    
    // Added this now 
    wp_reset_query() ; 
    }
    ?></div>
  • You need to create variable that keep a running total. For example:

    
    $counter = 0;
    while ($query->have_posts()) {
      $query->the_post();
      if (get_field('field_name') == 1) {
        $counter++;
      }
    }
    echo $counter;
    
  • Thanks John, but this gives me just the amount of posts, correct?

    What I need is something like attached

  • You would have to create one variable for each field you want to total. Then add the value of the field to the total on each loop. I could not really tell what you are looking to total looking at the code.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.