• Resolved enkayes

    (@enkayes)


    Hello,

    I currently have a function that calls and adds a custom field from each post. (example: Post #1: apples=1 Post #2 apples=1 total apples=2)

    Whenever I got into a specific post, it negates getting the custom fields from all posts and gets in from the post I’m currently in instead. (example: apples=1)

    Is there anyway around this? Does this code look correct for what I want to accomplish?

    What I want (Home Page)
    What I’m getting on the posts

    Looping code:

    <div id="stats-current-weight">
    
    						<?php
    						$totalLostWeight = 0;
    						$startWeight = 297.0;
    						while (have_posts()) : the_post();
    						    $postmeta = get_post_custom($post->ID);
    						    $totalLostWeight += (int) $postmeta['weight-lost'][0];
    						endwhile;
    						$lost = $startWeight + $totalLostWeight;
    						echo "<p class='stats-number'>" . $lost . "</p>";
    
    						?>	
    
    						<p class="stats-text">current weight</p>
    
    					</div>
    
    					<div id="stats-weight-lost">
    						<?php
    						$totalLost = 0;
    						while (have_posts()) : the_post();
    						    $postmeta = get_post_custom($post->ID);
    						    $totalLost -= (float) $postmeta['weight-lost'][0];
    						endwhile;
    						echo "<p class='stats-number'>" . $totalLost . "</p>";
    						?>
    
    						<p class="stats-text">weight lost</p>
    
    					</div>
    
    					<div id="stats-exercise-time">
    
    						<?php
    						function formatTime($total){
    						    $days = $hours = $minutes = 0;
    						    // 1440 = number minutes in a day.
    						    if($total > 1440){
    						         $days = intval($total / 1440);
    						         $total = $total % 1440;
    						    }
    						    if($total > 60){
    						        $hours = intval($total / 60);
    						        $minutes = $total % 60;
    						    }
    
    						    if ($days < 10) {
    						    	$days = "0" . $days;
    						    }
    
    						    if ($hours < 10) {
    						    	$hours = "0" . $hours;
    						    }
    
    						    if ($minutes < 10) {
    						    	$minutes = "0" . $minutes;
    						    }
    
    						    return "$days:$hours:$minutes";
    						}
    
    						$total = 0;
    						while (have_posts()) : the_post();
    						    $postmeta = get_post_custom($post->ID);
    						    $total += (int) $postmeta['wii-time'][0];
    						endwhile;
    
    						echo "<p class='stats-number'>" . formatTime($total) . "</p>";
    
    						?>
    
    						<p class="stats-text" id="exercise-text">total exercise time</p>
    
    					</div>

    Thanks!

    –nks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter enkayes

    (@enkayes)

    I’m on the right track with this:

    <?php
    
     $querydetails = "
       SELECT wposts.*
       FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
       WHERE wposts.ID = wpostmeta.post_id
       AND wpostmeta.meta_key = 'weight-lost'
       AND wposts.post_status = 'publish'
       AND wposts.post_type = 'post'
       ORDER BY wposts.post_date DESC
     ";
    
     $pageposts = $wpdb->get_results($querydetails, OBJECT)
    
     ?>
    
     <?php if ($pageposts):
     foreach ($pageposts as $post):
           setup_postdata($post); ?>
    
                   <div>
                   <?php echo get_post_meta($post->ID, 'weight-lost', true); ?>
                   </div>
    
     <?php endforeach;
    endif; ?>

    just have to figure out how to add the values.

    –nks

    Thread Starter enkayes

    (@enkayes)

    Solved!

    <div id="stats-current-weight">
    
    						<?php
    				$currentWeightArray = array();
    				$meta_key = 'weight-lost';
    				$currentWeightArray = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key));
    				$currentWeightArrayTotal = 297 + array_sum( $currentWeightArray );
    				echo $currentWeightArrayTotal;
    				?>	
    
    						<p class="stats-text">current weight</p>
    
    					</div>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Getting custom fields from all posts within a post’ is closed to new replies.