1

I'm trying to do a query in my index.php page to get some posts from my blogue category.

In my other pages this query works perfectly but on the index.php returns the most recent post 3 times.

They query:

$args = array(
    'posts_per_page'   => 3,
    'offset'           => 0,
    'cat'         => '',
    'category_name'    => 'blogue',
    'orderby'          => 'date',
    'order'            => 'DESC',
    'include'          => '',
    'exclude'          => '',
    'meta_key'         => '',
    'meta_value'       => '',
    'post_type'        => 'post',
    'post_mime_type'   => '',
    'post_parent'      => '',
    'author'       => '',
    'author_name'      => '',
    'post_status'      => 'publish',
    'suppress_filters' => true,
    'fields'           => '',

);
$wqBlog = get_posts($args); 

the html:

<section class="container"> 
<div class="cards display--flex ">
            <?php foreach ($wqBlog as $blogPost): setup_postdata( $blogPost ); ?>
            <div class="card">
                <?php if(has_post_thumbnail( ))  : ?>
                <div class="card__image display--flex">
                    <!--imagem -->
                    <?php the_post_thumbnail( ); ?>
                </div> <!-- imagem -->
                <?php else : ?>
                <div class="card__image__none">
                </div>
                <?php endif;?>
                <div class="card__container">
                    <p class="card__container__subtitle">
                        <?php echo get_the_date(); ?>
                    </p>

                    <h2 class="card__container__title">
                        <a href="<?php the_permalink(); ?>">
                            <?php the_title(); ?>
                        </a>
                    </h2>
                </div>
            </div>
            <?php endforeach; ?>         
        </div> 

</section>

1 Answer 1

1

Instead of get_posts, why don't you use WP_Query instead?

I removed a few arguments because they are the default and can be omitted.

$args = array(
    'posts_per_page'   => 3,
    'category_name'    => 'blogue',
    'suppress_filters' => true,
);
$wqBlog = new WP_Query($args);

And in your HTML

<?php if( $wpBlog->have_posts() ): ?>
<section class="container"> 
<div class="cards display--flex ">
            <?php while ( $wqBlog->have_posts() ): $wpBlog->the_post(); ?>
            <div class="card">
                <?php if(has_post_thumbnail( ))  : ?>
                <div class="card__image display--flex">
                    <!--imagem -->
                    <?php the_post_thumbnail( ); ?>
                </div> <!-- imagem -->
                <?php else : ?>
                <div class="card__image__none">
                </div>
                <?php endif;?>
                <div class="card__container">
                    <p class="card__container__subtitle">
                        <?php echo get_the_date(); ?>
                    </p>

                    <h2 class="card__container__title">
                        <a href="<?php the_permalink(); ?>">
                            <?php the_title(); ?>
                        </a>
                    </h2>
                </div>
            </div>
            <?php endwhile; wp_reset_postdata(); ?>         
        </div> 

</section>
<?php endif; ?>
5
  • I was using wp_query but it was causing me an error , not a fatal one, but one that was appearing on my console (something about the version of my wordpress)... I switched the while cicle for a for each one due to the infinite cycles i would cause sometimes... but i guess i'll give it a try thanks for your answer ! Oct 11, 2019 at 7:55
  • WP_Query can't put things in your browser console, that will be something in javascript doing that, not PHP
    – Tom J Nowell
    Oct 14, 2019 at 14:25
  • Did that work @PedroFerreira? Nov 26, 2019 at 16:50
  • yes , but i was also not closing one of my while cycles ! thanks a lot sorry i completly forgot !! Nov 26, 2019 at 17:14
  • Cool! I'm glad to help. Please mark this as the accepted answer if that was the case. Thank you. Nov 27, 2019 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.