admin管理员组

文章数量:1122846

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 question

I 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 question

I 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 1
Add a comment  | 

1 Answer 1

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