I have a form that stores various numerical values in the database. I assumed they were in the wp_posts table but I don’t see any of the data there.
If I want to retrieve a field value from each post and add them all together, where do I access that data in the DB?
Data for posts is stored in the _postmeta table. Data for users is stored in the _usermeta table. Data for options pages and taxonomies is stored in the _options table.
This is the code I’m trying to hack together. This returns a list of values for each field. Now I need to figure out how to add (sum) them together.
Any ideas?
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'pov_travel'
));
?>
<?php
if( $posts ):
$dla = 0;
?>
<?php foreach( $posts as $post ): ?>
<?php
$dla = get_field ('dla');
echo $dla;
?>
<?php endforeach; ?>
<?php endif; ?>
instead of $dla = get_field ('dla');
you can do something like this $dla += get_field ('dla');
Thanks John,
For anyone else that may be interested, I also had to move the echo outside the loop. Working code below.
<?php
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'pov_travel'
));
if( $posts ):;
$dla = 0;
foreach( $posts as $post ):
$dla += get_field ('dla');
endforeach;
echo $dla;
endif;
?>