Skip to main content
added 38 characters in body
Source Link
            <?php
            $args = array(
                'post_type' => 'post',
                'post_status' => 'any',
                'category' => 3,
                'posts_per_page' => 4);
            $wpdb =$query_var= new WP_Query($args);
            if ($wpdb$query_var->have_posts()):
                while ($wpdb$query_var->have_posts()):
                    $wpdb$query_var->the_post(); ?>
                    <div class="patta p-4 col-lg-3 col-md-6">
                        <!-- FIX THIS -->
                        <img class="card-img-top"
                             src="<?php the_post_thumbnail(); ?>"
                             alt="<?php the_post_thumbnail_caption() ?>"/>
                        <h4><b><?php the_title(); ?></b><br></h4>
                        <p>
                            <a href="#">Link</a>
                        </p>
                    </div>
                <?php
                endwhile;
                wp_reset_postdata();
            endif;
            ?>
<?php
            $args = array(
                'post_type' => 'post',
                'post_status' => 'any',
                'orderby' => 'post_date',
                'order' => 'DESC',
                'posts_per_page' => 4);
            $wpdb =$query_var= new WP_Query($args);
            if ($wpdb$query_var->have_posts()):
                while ($wpdb$query_var->have_posts()):
                    $wpdb$query_var->the_post(); ?>
                    <div class="patta p-4 col-lg-3 col-md-6">
                        <!-- FIX THIS -->
                        <img class="card-img-top"
                             src="<?php if (the_post_thumbnail()): the_post_thumbnail(); else:echo 'https://lamasec.pythonanywhere.com/static/img/vulnhub.png';endif; ?>"
                             alt="<?php the_post_thumbnail_caption() ?>"/>
                        <h4><b><?php the_title(); ?></b><br></h4>
                        <p>
                            <a href="#">Link</a>
                        </p>
                    </div>
                <?php
                endwhile;
                wp_reset_postdata();
                else: echo 'not working';
            endif;
            ?>
            <?php
            $args = array(
                'post_type' => 'post',
                'post_status' => 'any',
                'category' => 3,
                'posts_per_page' => 4);
            $wpdb = new WP_Query($args);
            if ($wpdb->have_posts()):
                while ($wpdb->have_posts()):
                    $wpdb->the_post(); ?>
                    <div class="patta p-4 col-lg-3 col-md-6">
                        <!-- FIX THIS -->
                        <img class="card-img-top"
                             src="<?php the_post_thumbnail(); ?>"
                             alt="<?php the_post_thumbnail_caption() ?>"/>
                        <h4><b><?php the_title(); ?></b><br></h4>
                        <p>
                            <a href="#">Link</a>
                        </p>
                    </div>
                <?php
                endwhile;
                wp_reset_postdata();
            endif;
            ?>
<?php
            $args = array(
                'post_type' => 'post',
                'post_status' => 'any',
                'orderby' => 'post_date',
                'order' => 'DESC',
                'posts_per_page' => 4);
            $wpdb = new WP_Query($args);
            if ($wpdb->have_posts()):
                while ($wpdb->have_posts()):
                    $wpdb->the_post(); ?>
                    <div class="patta p-4 col-lg-3 col-md-6">
                        <!-- FIX THIS -->
                        <img class="card-img-top"
                             src="<?php if (the_post_thumbnail()): the_post_thumbnail(); else:echo 'https://lamasec.pythonanywhere.com/static/img/vulnhub.png';endif; ?>"
                             alt="<?php the_post_thumbnail_caption() ?>"/>
                        <h4><b><?php the_title(); ?></b><br></h4>
                        <p>
                            <a href="#">Link</a>
                        </p>
                    </div>
                <?php
                endwhile;
                wp_reset_postdata();
                else: echo 'not working';
            endif;
            ?>
            <?php
            $args = array(
                'post_type' => 'post',
                'post_status' => 'any',
                'category' => 3,
                'posts_per_page' => 4);
            $query_var= new WP_Query($args);
            if ($query_var->have_posts()):
                while ($query_var->have_posts()):
                    $query_var->the_post(); ?>
                    <div class="patta p-4 col-lg-3 col-md-6">
                        <!-- FIX THIS -->
                        <img class="card-img-top"
                             src="<?php the_post_thumbnail(); ?>"
                             alt="<?php the_post_thumbnail_caption() ?>"/>
                        <h4><b><?php the_title(); ?></b><br></h4>
                        <p>
                            <a href="#">Link</a>
                        </p>
                    </div>
                <?php
                endwhile;
                wp_reset_postdata();
            endif;
            ?>
<?php
            $args = array(
                'post_type' => 'post',
                'post_status' => 'any',
                'orderby' => 'post_date',
                'order' => 'DESC',
                'posts_per_page' => 4);
            $query_var= new WP_Query($args);
            if ($query_var->have_posts()):
                while ($query_var->have_posts()):
                    $query_var->the_post(); ?>
                    <div class="patta p-4 col-lg-3 col-md-6">
                        <!-- FIX THIS -->
                        <img class="card-img-top"
                             src="<?php if (the_post_thumbnail()): the_post_thumbnail(); else:echo 'https://lamasec.pythonanywhere.com/static/img/vulnhub.png';endif; ?>"
                             alt="<?php the_post_thumbnail_caption() ?>"/>
                        <h4><b><?php the_title(); ?></b><br></h4>
                        <p>
                            <a href="#">Link</a>
                        </p>
                    </div>
                <?php
                endwhile;
                wp_reset_postdata();
                else: echo 'not working';
            endif;
            ?>
Source Link

Unable to reset post data in wordpress custom query

Edit - I already tried wp_reset_query() it doesn't work, the first loop executes as intended but the second loop just jumps to the else and I get not working I am using 2 custom WP_Query loops in one page, the first is to get posts from a certain category, and the second is just getting the posts by dates,

here's the code

            <?php
            $args = array(
                'post_type' => 'post',
                'post_status' => 'any',
                'category' => 3,
                'posts_per_page' => 4);
            $wpdb = new WP_Query($args);
            if ($wpdb->have_posts()):
                while ($wpdb->have_posts()):
                    $wpdb->the_post(); ?>
                    <div class="patta p-4 col-lg-3 col-md-6">
                        <!-- FIX THIS -->
                        <img class="card-img-top"
                             src="<?php the_post_thumbnail(); ?>"
                             alt="<?php the_post_thumbnail_caption() ?>"/>
                        <h4><b><?php the_title(); ?></b><br></h4>
                        <p>
                            <a href="#">Link</a>
                        </p>
                    </div>
                <?php
                endwhile;
                wp_reset_postdata();
            endif;
            ?>

second loop

<?php
            $args = array(
                'post_type' => 'post',
                'post_status' => 'any',
                'orderby' => 'post_date',
                'order' => 'DESC',
                'posts_per_page' => 4);
            $wpdb = new WP_Query($args);
            if ($wpdb->have_posts()):
                while ($wpdb->have_posts()):
                    $wpdb->the_post(); ?>
                    <div class="patta p-4 col-lg-3 col-md-6">
                        <!-- FIX THIS -->
                        <img class="card-img-top"
                             src="<?php if (the_post_thumbnail()): the_post_thumbnail(); else:echo 'https://lamasec.pythonanywhere.com/static/img/vulnhub.png';endif; ?>"
                             alt="<?php the_post_thumbnail_caption() ?>"/>
                        <h4><b><?php the_title(); ?></b><br></h4>
                        <p>
                            <a href="#">Link</a>
                        </p>
                    </div>
                <?php
                endwhile;
                wp_reset_postdata();
                else: echo 'not working';
            endif;
            ?>

I am using wp_reset_postdata(); but it doesn't seem to be working.