Add the sum of a variable inside a wordpress while loop - php

I am using wordpress and ACF for a custom field that has a number in it on each post.
I am trying to query a set of posts and then get the sum of the custom field of the posts queried.
$total_income = 0;
$args = array(
'post_type' => 'acct_income',
'year' => '2023',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$acct_amount = the_field('acct_income_amount');
$total_income += $acct_amount;
echo $total_income;
endwhile;
endif;
This just displays the custom field number in each post and doesn't add them together.

Related

How to increment posts within a specific category

I am returning all posts on the page. Every six posts I want to incrementally show a post from a specific category. Every time this comes to pass I would like to increment up in the loop so there are no repeats of the same post within that specific category on the page.
I've successfully gotten the posts from that category showing on every sixth post on the page. I am just unable to get the loop to work so that it shows incrementally the next post in the category on every sixth spot on the page. Currently it just shows the same first post in the array.
<?php while ($query->have_posts()) {
if( $query -> post_count > 0 ) {
$postnum = 0;
foreach( $query -> posts as $post ) {
$postnum++;
if( $postnum%5 == 0 ) {
$args = array( 'cat' => 1824, 'posts_per_page' => 1, );
query_posts( $args );
$current_post = 0;
while ( have_posts() ) : the_post();
$current_post++;
echo "CTA Card Specific Info";
endwhile;
}
$query->the_post();
?>```
Instead of using query_posts(), you could nest anther WP_Query inside the first and use the offset parameter to skip over the posts you've already output. I haven't tested this code, but something like the following could work:
$post_count = 0;
$category_count = 0; // for determining offset
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'category__not_in' => 1824, // or something like this to prevent duplicates
);
$post_query = new WP_Query ( $args );
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) : $post_query->the_post();
$post_count++;
echo "Regular Post Here";
if ( $post_count % 6 === 0 ) {
$args = array(
'cat' => 1824,
'posts_per_page' => 1,
'offset' => $category_count,
);
$category_query = new WP_Query( $args );
$category_count++;
if ( $category_query->have_posts() ) {
while ( $category_query->have_posts() ) : $category_query->the_post();
echo "CTA Card Specific Info";
endwhile; $post_query->reset_postdata();
}
}
endwhile;
}
Once you've finished with the inside loop, make sure you call reset_postdata() to change the context of the query back to the main query.
It's also worth noting that using offset can mess up your pagination. I don't think that will come into play here, but if you notice pagination issues that could be the culprit.

Removing duplicate data from Array loop

Currently I am trying to remove duplicate iterations from my post loop, and it seems that my code is working perfectly the only issue is that my &posts_per_page=21 declare is no longer present, it displays only 3 results for example (which are the unique titles)
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'orderby' => 'title'
);
$the_query = new WP_Query( $args );
$unique_artist = array();
query_posts($query_string.'&cat=7&posts_per_page=21'); if ( have_posts() ) : while ( have_posts() ) : the_post();
// title example for post "Artist - Song Name"
$title = get_the_title();
$artist = explode('-', $title);
$main_artist = $artist[0];
$aArtist = $main_artist;
if( ! in_array( $aArtist, $unique_artist ) ) :
$unique_artist[] = $aArtist;
the_title();
endif; endwhile; endif;
How can I make it so that no matter what, my post_per_page will display the 21 desired results? Instead of only showing the unique titles, when we remove the duplicates..

Wordpress woocommerce multiple orderby args loop

Does anyone know how to incorporate a while loop into a basic order by filter? I have the following basic filter in my functions.php page that orders my products by the featured products and it works:
add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
if(!$_GET['orderby']) {
$args = array(
'orderby' => array( 'meta_value' => 'DESC' ),
'meta_key' => '_featured'
);
return $args;
}
}
My problem is I want to use some more advanced while loops to try and get multiple different arguments tied into the same args. I am trying to show my featured products first, then load that into an array then get my next loop and only show the products that are 6 months old or younger and add that to my array, then show everything else and sort them by menu_order and title and add it to my array as shown below:
add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $my_post_array ) {
if(!$_GET['orderby']) {
//First loop - show featured
$args['orderby'] = array( 'meta_value' => 'DESC' );
$args['meta_key'] = '_featured';
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$post_id = get_the_ID();
$my_post = my_post_function($post_id);
//Store the items in an array
$my_post_array [] = $my_post;
query_posts($args);
endwhile;
wp_reset_query();
//Second loop - Show newer than 6 months
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'date_query' => array(
array(
'before' => '6 months ago',
),
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$post_id = get_the_ID();
$my_post = my_post_function($post_id);
//Store the items in an array
$my_post_array [] = $my_post;
query_posts($args);
endwhile;
wp_reset_query();
//Third loop - show everything else and sort by menu_order and title
$args['orderby'] = array( 'menu_order' => 'ASC', 'title' => 'ASC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$post_id = get_the_ID();
$my_post = my_post_function($post_id);
//Store the items in an array
$my_post_array [] = $my_post;
query_posts($args);
endwhile;
wp_reset_query();
//Remove duplicate entries from the array
array_unique ( $my_post_array, SORT_STRING );
}
}
Then the last thing I do is remove my duplicates. Only problem is it does not work and I am not sure where I am going wrong, I have not done something like this before so I could be working in the wrong area or missing something obvious. Anyone have ideas on how I can trouble shoot this or does anyone see something that may be causing me issues? Thanks for the help, still learning and trying to improve!

WP Woocommerce array to show products based on title

I am trying to use the following array to show the products in category based on page title. If the title is CUHA, I want it to show product category CUHA-подови. However, I can't seem to make it work.
This is the code that I am using. It works okay, but when I try to use get_the_title() in the array it crashes. I know that the problem is in my array skills.
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => 'get_the_title() . - . подови' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().'';
endwhile;
wp_reset_query();
?>

Custom post type exclude taxonomy posts

I have a cutom post type named : art_design and i have a taxonomy called artdesign_mainslider, in one loop i have only posts from the taxonomy artdesign_mainslider and in the other loop i have posts from custom post type art_design but i want to exclude the taxonomy ?
1st loop:
<?php
$args = array( 'artdesign_mainslider' => 'art-design-featured-post', 'posts_per_page' => 5 );$loop = new WP_Query( $args );while ( $loop->have_posts() ) : $loop->the_post(); ?><?php endwhile; ?>
2nd loop ( i want to exclude artdesign_mainslider):
<?php
$args = array( 'post_type' => 'art_design', 'posts_per_page' => 10, );$loop = new WP_Query( $args );while ( $loop->have_posts() ) : $loop->the_post(); ?><?php endwhile; ?>
You have to use the tax_query parameter. See http://www.wpexplorer.com/exclude-taxonomy-wordpress/ for a perfect example of what you're trying to do and http://codex.wordpress.org/Class_Reference/WP_Query for the reference for tax_query.

Categories