Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1122846
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 months ago.
Improve this questionI have the following code
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$party_size = get_field('reservation_party_size', $post_id);
$numbers .= $party_size;
}
}
return $numbers;
And this returns all party sizes after each other like this: 12345 (so without a space of comma).
I just want to sum all these numbers to a total (in this example: 15)
What I tried:
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$party_size = get_field('reservation_party_size', $post_id);
$numbers .= $party_size;
}
}
$array = array($numbers);
return array_sum($array);
Also tried:
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$party_size = get_field('reservation_party_size', $post_id);
$numbers .= $party_size;
}
}
$list = implode(', ', $numbers);
$array = array($list);
return array_sum($array);
And I can go on and on.... Getting crazy here.
So in short: I have various posts with a custom field called PART_SIZE and I just want to add up all PARTY SIZE field values so I know how many TOTAL people will come over.
Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 months ago.
Improve this questionI have the following code
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$party_size = get_field('reservation_party_size', $post_id);
$numbers .= $party_size;
}
}
return $numbers;
And this returns all party sizes after each other like this: 12345 (so without a space of comma).
I just want to sum all these numbers to a total (in this example: 15)
What I tried:
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$party_size = get_field('reservation_party_size', $post_id);
$numbers .= $party_size;
}
}
$array = array($numbers);
return array_sum($array);
Also tried:
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$party_size = get_field('reservation_party_size', $post_id);
$numbers .= $party_size;
}
}
$list = implode(', ', $numbers);
$array = array($list);
return array_sum($array);
And I can go on and on.... Getting crazy here.
So in short: I have various posts with a custom field called PART_SIZE and I just want to add up all PARTY SIZE field values so I know how many TOTAL people will come over.
Share Improve this question asked Sep 17, 2024 at 12:09 iWebbersiWebbers 11 Answer
Reset to default 1.=
does string concatention, eg, 'hello ' . 'world'
gets you hello world
.
To add, use +
instead of .
:
// Set a default for $numbers.
$numbers = 0;
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$party_size = get_field('reservation_party_size', $post_id);
$numbers += intval( $party_size );
}
}
return $numbers;
I use intval()
to make sure that $party_size
is an integer.
More reading: PHP Assignment Operators
本文标签: phpAdd up all numbers from a WordPress query
版权声明:本文标题:php - Add up all numbers from a WordPress query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736291531a1928743.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论