Getting the total from sub fields located in CPT - wordpress

I am trying to collect a series of numbers inside a repeatable subfield in a custom post type, and then output the sum in a line of text. I've written the code below, but I have been having very little success in getting it to work.
Would appreciate if someone could have a look and amend for my education. Thanks
<?php
$args = array( 'post_type' => 'distribution-post' );
$amount = new WP_Query( $args );
$total = 0;
while ( have_rows('arr_dist') ) : the_row();
$total += intval( get_sub_field('amount'));
endwhile;
echo $total;
wp_reset_query();
?>

Try the below code. you have missed using have_post before using have_row.
$args = array( 'post_type' => 'distribution-post' );
$amount = new WP_Query( $args );
$total = 0;
if ( $amount->have_posts() ) {
while ( $amount->have_posts() ) : $amount->the_post();
while ( have_rows('arr_dist') ) : the_row();
$total += intval( get_sub_field('amount'));
endwhile;
endwhile;
wp_reset_postdata();
}
echo $total;
wp_reset_query();

<?php
$total = 0;
while(the_repeater_field('repeaterfield name' )):
$total += get_sub_field('repeatersubfield name' );
endwhile;
echo $total;
?>
change repeater field and sub-field as per your need.

Related

WordPress 6 function show_posts shows the same content for each

it loops correctly 10 times but with wrong content. it alloys using the content with id 11
show_posts with 10 posts (thats correct) but always first content
my function show_posts in Theme.php :
public function show_posts() { # show_posts220914() {
$args = array(
'numberposts' => 200
);
$my_posts = get_posts( $args );
if( ! empty( $my_posts ) ){
foreach ( $my_posts as $post ){
get_template_part( 'content','content');
}
}
}
so result in http://localhost/wordpress/ is
<li>11</li><li>11</li><li>11</li><li>11</li><li>11</li><li>11</li><li>11</li><li>11</li><li>11</li><li>11</li>
show_posts with correct constant but to less posts:
in this next try i have correct constant but to less posts:
public function show_posts(){
global $wp_query;
$args = array_merge( $wp_query->query_vars, ['posts_per_page' => 200 ] ); # has no effect here
query_posts( $args ); # has no effect here
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$post= the_post();
get_template_part( 'content','content');
endwhile;
endif;
}
so result in http://localhost/wordpress/ is
<li>11</li><li>12</li><li>13</li><li>14</li><li></li>
my content.php:
<li>
<?php the_title(); ?>
</li>
my home.php:
<ol>
<?php
do_action( 'home_content' );
?>
</ol>
i call it with http://localhost/wordpress/.
i don't use any pages. means http://localhost/wordpress/wp-admin/edit.php?post_type=page tells me No pages found.
i found inspiration here:
get_template_part/#parameters
show-the-same-post
query_posts('posts_per_page=20')
i using WordPress 6.0.2
any idea?
Your second WordPress loop looks like it could almost work ... I'm not sure what $wp_query->query_vars could contain that you are merging with your post_per_page constraint.
Try this, more standard loop (and/or read about the loop on WordPress' Codex):
function show_post(){
$args = [
'posts_per_page' => 10,
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
];
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
get_template_part( 'content','content');
endwhile;
endif;
// Restore original Post Data
wp_reset_postdata();
}
i get this correct output
<li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li>
if i use this:
Theme.php :
public function show_posts_so() {
$posts = get_posts(array('numberposts' => -1));
if ( count($posts)>0 ) :
foreach($posts as $this_special_post){
get_template_part( 'content','content', $this_special_post);
}
endif;
}
content.php :
<li><?php echo $args->ID ?></li>
functions.php :
add_action( 'home_content', [$theme ,'show_posts_so']);
home.php :
<?php do_action( 'home_content' ); ?>
it helps me reading this: What is the real purpose of magic method __set_state in PHP?
and the use of the var_export command sometimes

wordpress - post thumbnail is not showing up

Id like to show the list of post thumbnails from specified user, using this code:
global $user;
$numposts = get_posts( array('author' => $user->ID) );
<?php foreach ($numposts as $numpost) {
the_post_thumbnail();
} ?>
Thumbnails not showing up though. Am I doing something wrong? This code is inserted in plugin php file. thx
Reworked and works:
$numposts = get_posts( array('author' => $user->ID) );
<?php
$my_query = new WP_Query( array( 'author' => $user->ID ) );
while ( $my_query->have_posts() ) : $my_query->the_post();
{
echo the_post_thumbnail();
}
endwhile;
?>
although not sure why it didnt work first time.

woocommerce search on product attribute added by ACF using wordpress default search

In Wordpress default search functionality i want to search woocommerce search on product attribute. Please guide me on this.
Thanks in advance.
You can do that by using product post meta. put the below code in no-products-found.php.
global $wpdb;
$item_code = get_search_query();
$sql = 'SELECT DISTINCT post_id FROM wp_postmeta WHERE meta_value LIKE "'.$item_code.'"';
$results = $wpdb->get_results($sql);
$pro = array();
foreach( $results as $result ){
$pro[] = $result->post_id;
}
if($pro){
$args = array( 'post_type' => 'product', 'post__in' => $pro );
$loop = new WP_Query($args);
woocommerce_product_loop_start();
woocommerce_product_subcategories();
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile; wp_reset_query();
woocommerce_product_loop_end();
} else { ?>
<p class="woocommerce-info"><?php _e( 'No products were found matching your selection.', 'woocommerce' ); ?></p>
<?php } ?>

Get author_id in wordpress

I'm having problems to get author_id in this code. Don't show nothing. Some can help me?
Entire code: (I'm trying get custom_post_type value of the same author of post.
<?php global $query_string;
query_posts( $query_string . '&orderby=rand' ); ?>
<?php while(have_posts()): the_post();
$author = get_the_author();
echo 'Autor:'.$author;
$ofertas = array();
// Second Loop
$i = 0;
$args = array(
'author' => $author_id,
'post_type' => 'ofertas'
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post(); // check if it has offers, if it has, loop over the offers
$ofertas[$i]['title'] = get_the_title(); // or anything you need
$i++;
endwhile; // close the
else: // if it has no post
continue; // we don't display the post
endif;
wp_reset_postdata();
?>
<?php
$blogusers = get_users('role=author');
foreach ($blogusers as $user) {
echo 'Author ID: '.$user->ID;
$author_id = $user->ID;
$ofertas = array();
// Second Loop
$i = 0;
$args = array(
'author' => $author_id,
'post_type' => 'ofertas'
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post(); // check if it has offers, if it has, loop over the offers
$ofertas[$i]['title'] = get_the_title(); // or anything you need
$i++;
endwhile; // close the
else: // if it has no post
continue; // we don't display the post
endif;
wp_reset_postdata();
} // end foreach
?>
Does this suit your requirements ?

Making custom WooCommerce loop

I'm using already designed theme for wordpress, and now instead of regular blog posts I would like to display WooCommerce products (which are custom post types I persume).
This is the current query with display loop:
<?php
$args = array(
//'posts_per_page' => '2',
'paged' => get_query_var('paged')
);
$homepage_query = new WP_Query($args);
?>
<?php //query_posts('posts_per_page=4&paged='.get_query_var('paged')); ?>
<?php if ( have_posts() ) : ?>
<?php while ( $homepage_query->have_posts() ) : $homepage_query->the_post(); ?>
<?php if($style == 'blog_style') { ?>
<div id="blog-style" class="post-box">
<?php get_template_part('content', 'blog'); ?>
</div>
<?php } else { ?>
<div class="post-box grid_4 <?php aero_post_box_class(); ?>">
<?php get_template_part('content', ''); ?>
</div>
<?php } ?>
<?php endwhile; ?>
Is there a way to add options to $args so the loop displays WooCommerce products? I'm also using pagination with this loop, which is required on this project, so that's why it's important to use this loop.
You should be able to access products through the loop, setting the post_type arg to product:
<?php
// Setup your custom query
$args = array( 'post_type' => 'product', ... );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>">
<?php the_title(); ?>
</a>
<?php endwhile; wp_reset_query(); // Remember to reset ?>
This is the proper way to re-create and customize the WooCommerce product loop:
if(!function_exists('wc_get_products')) {
return;
}
$paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1; // if your custom loop is on a static front page then check for the query var 'page' instead of 'paged', see https://developer.wordpress.org/reference/classes/wp_query/#pagination-parameters
$ordering = WC()->query->get_catalog_ordering_args();
$ordering['orderby'] = array_shift(explode(' ', $ordering['orderby']));
$ordering['orderby'] = stristr($ordering['orderby'], 'price') ? 'meta_value_num' : $ordering['orderby'];
$products_per_page = apply_filters('loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page());
$products_ids = wc_get_products(array(
'status' => 'publish',
'limit' => $products_per_page,
'page' => $paged,
'paginate' => true,
'return' => 'ids',
'orderby' => $ordering['orderby'],
'order' => $ordering['order'],
));
wc_set_loop_prop('current_page', $paged);
wc_set_loop_prop('is_paginated', wc_string_to_bool(true));
wc_set_loop_prop('page_template', get_page_template_slug());
wc_set_loop_prop('per_page', $products_per_page);
wc_set_loop_prop('total', $products_ids->total);
wc_set_loop_prop('total_pages', $products_ids->max_num_pages);
if($products_ids) {
do_action('woocommerce_before_shop_loop');
woocommerce_product_loop_start();
foreach($products_ids->products as $featured_product) {
$post_object = get_post($featured_product);
setup_postdata($GLOBALS['post'] =& $post_object);
wc_get_template_part('content', 'product');
}
wp_reset_postdata();
woocommerce_product_loop_end();
do_action('woocommerce_after_shop_loop');
} else {
do_action('woocommerce_no_products_found');
}
Using the code above, you would customize the wc_get_products() arguments to get the IDs of the products you want (if you have specific criteria). Once that code is in place, all the features of a native WooCommerce loop will be available to you—pagination, ordering, etc. This method is superior to WP_Query and get_posts() because those two methods can break.
I've written a more detailed blog post about custom WooCommerce loops here: https://cfxdesign.com/create-a-custom-woocommerce-product-loop-the-right-way/
You can Also get Category using thi code
$terms = get_terms('product_cat');
foreach ($terms as $term) {
$term_link = get_term_link( $term, 'product_cat' );
echo '<li>' . $term->name . '</li>';
}
If You want only parent category then
wp_list_categories('taxonomy=product_cat&orderby=order&title_li=&depth=1');

Resources