skip to Main Content

i make two wp query loop one is within the loop something went wrong i cant figure out here is the code
the inner wp query for image cpt related with main query cpt.

$maincontest = new WP_Query(array(
                'posts_per_page' => 3,
                'post_type' => 'contest',
                'post_status' => 'any'
            ));
            while($maincontest->have_posts()){
                $maincontest->the_post(); ?>
            <div class = "listloopmain">    
                <a href="<?php the_permalink(); ?>"> 
                    <div class = "listcontestitem">
                        <div class = "listimage">
                            <?php
                                $toplogo = new WP_Query(array(
                                    'posts_per_page' => 1,
                                    'post_type' => 'logo',
                                    'orderby' => 'rand'
                                ));
                                while($toplogo->have_posts()){
                                    $toplogo->the_post();
                                    $image = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
                                    <div class ="listim">
                                        <img src="<?php echo $image ?>"/>   
                                    </div>
                                <?php }
                                wp_reset_postdata();
                            ?>
                        </div>
                        <div class = "title_des">
                            <div class = "listtitle">
                                <h4>
                                    <?php the_title(); ?>
                                </h4>
                            </div>
                            
                            <div class = "listdescription">
                                <?php echo wp_trim_words(get_the_content(), 20); ?>
                            </div>
                        </div>
                        <div class = "listcontestprice">
                            $ <?php the_field('price'); ?>
                        </div>
                        <div class = "listtime">
                            5 Days
                        </div>
                                                            
                    </div>
                </a>
            </div>
                                
                <?php } ?>
            </div>
                <div class ="my-pagination">
                <?php           
                    echo paginate_links(array(
                        'prev_text' => __( '<' ),
                        'next_text' => __( '>' )
                    )); 
                    wp_reset_postdata();            
                ?>

Problem is main query permalink is correct but content is same for each loop. Image query is working fine. Can anybody help?

Thanks in advance

2

Answers


  1. Chosen as BEST ANSWER

    hi i got the solution want main custom query only for showing private post. achieved by filtering pre_get_posts and set method. thank u all

    function contest_adjust_query($query){
        if (!is_admin() AND is_post_type_archive('contest') AND $query->is_main_query()) {
            $query->set('post_status', 'private');
            $query->set('post_status', array('publish', 'private'));
        }
    }
    
    add_action('pre_get_posts', 'contest_adjust_query');
    

  2. At this part of your code –

    while($toplogo->have_posts()){
        $toplogo->the_post();
        $image = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
        <div class ="listim">
            <img src="<?php echo $image ?>"/>   
        </div>
    <?php }
    **wp_reset_postdata();**
    

    Use $toplogo->reset_postdata();

    Reference for the reset_postdata function you can find here – https://developer.wordpress.org/reference/classes/wp_query/reset_postdata/.

    Also, here’s a nice article explaining how it’s used – https://neliosoftware.com/blog/the-problems-of-using-nested-loops-in-wordpress/.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search