yasin
yasin

Reputation: 79

In wordpress how to get post loop inside another loop

I want to get latest post based on author loop. this is my code:

function display_artist_func() {
    $out_args = array(
        'post_type'   => 'product',
        'author'        =>  1,
        'showposts'   => 3
    );
$out_loop = new WP_Query($out_args);

while ($out_loop->have_posts()) :
    
    ?>
    <div><?php echo get_avatar( get_the_author_email(), '80' ); ?> </div>
    <?php
    
    //$product = wc_get_product( $product->id );
    $args = array(
        'post_type'   => 'product',
        'showposts'   => 10,
        'orderby'     => 'date',
        'order'       => 'DESC'
    );

    $loop = new WP_Query( $args );
    
    while ( $loop->have_posts() ) : $loop->the_post(); global $product;$author  = get_user_by( 'id', $product->post->post_author ); ?>

        <li>    
            <?php 
                if ( has_post_thumbnail( $loop->post->ID ) ) 
                    echo get_the_post_thumbnail( $loop->post->ID, 'shop_catalog' ); 
                else 
                    echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" width="65px" height="115px" />'; 
            ?>
            <h3><?php the_title(); ?></h3>

            <h4><?php echo $author->display_name;?></h4>
            <div><?php echo get_avatar( get_the_author_email(), '80' ); ?> </div>


            <?php 
                //echo $product->get_price_html(); 
                //woocommerce_template_loop_add_to_cart( $loop->post, $product );
            ?>    
        </li>

<?php 
    endwhile;
endwhile;
    wp_reset_query(); 

}
add_shortcode( 'display_artist', 'display_artist_func' );

there is 2 loop:

outside loop get the 3 author.

inside loop get the 3 post based on author from outside loop.

in outside loop a want to display author avatar.

but this code not work.

Upvotes: 0

Views: 147

Answers (1)

JBoss
JBoss

Reputation: 776

Your problem is that you're never setting up the post data in your outer loop. It looks to me like this code creates an endless loop? The function get_the_author_email() is actually deprecated, but what its doing is using global $authordata.

In your inner loop, you call:

$loop->the_post();

which is going to take your current query, setup postdata for the active post and then remove it so it no longer returns in your call to have_posts(). You're missing a call to:

$out_loop->the_post();

And so your postdata isn't setup, and you're going to create an endless loop.

NOW with all that said

To be perfectly honest your code doesn't look like it makes a lot of sense. Your 'outer loop' is querying products with one specific hard-coded author ID, but the only thing that outer post is used for IS to get the author email. The way its written - the author is going to be the same for every run of $out_loop.

So I think you still may be missing some logic here - I don't know from your description exactly how its supposed to work, but as is - if you're just looping through products, there's no need for two loops here.

Upvotes: 1

Related Questions