WordPress Get Posts not working as expected

I’m trying to display 6 posts from a specific custom post type in WordPress. Everything is working, except for when I remove the line “$wp_query = new WP_Query();”. When missing that line, 20 posts gets displayed in alphabetical order and I have no idea why. wp_reset_postdata() or wp_reset_query() don’t seem to do anything.

<?php $wp_query = new WP_Query( ); ?>

<div class="blog-footer row margin-top">
    <?php

    global $post;
    $args = array( 'post_type' => 'blog', 'posts_per_page' => 6 );
    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
            <div class="col-md-2 col-sm-4">
                <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail('thumbnail'); ?>
                    <h4><?php the_title(); ?></h4>
                </a>
            </div>
    <?php endforeach; 
    wp_reset_postdata();?>
</div>

Happy to get any inputs on why this behaves the way it does. Appreciate it!

Read more here: WordPress Get Posts not working as expected

Leave a Reply

Your email address will not be published. Required fields are marked *