0

I'm trying to run multiple wp_queries in my main homepage ..

is this a good practice ?

$argsPort= array(
    'posts_per_page'   => 6,
    'category_name'    => 'portefolio',
    'suppress_filters' => true,
);
$wqPort = new WP_Query($argsPort);

$argsServices = array(
    'posts_per_page'   => 6,
    'category_name'    => 'servicos',
    'suppress_filters' => true,
);
$wqServices = new WP_Query($argsServices);

$argsClients= array(
    'posts_per_page'   => 3,
    'category_name'    => 'clientes',
    'suppress_filters' => true,
);
$wqClients = new WP_Query($argsClients);

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

i'm already using two loops but only one of them works, the the other just doesn't return anything they're both the same but one uses $wqPort and the other one uses $wqBlog, only wqLog works

  <?php foreach ($wqPort as $projeto) : setup_postdata( $projeto );?>
                <li class="glide__slide">
                <div class="card">
            <a href="<?php the_permalink(); ?>">

                <?php if(has_post_thumbnail( ))  : ?>
                <div class="card__image display--flex"
                    style="background-image: url('<?php echo wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); ?>');">
                    <!--imagem -->

                </div> <!-- imagem -->
                <?php else : ?>
                <div class="card__image__none">
                </div>
                <?php endif;?>
                <!--  -->
                <div class="card__container">
                    <p class="card__container__subtitle">
                        <?php the_tags('', ', ', '<br />'); ?>
                    </p>

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

                    </h2>

                </div>
            </a>
        </div>
                </li>
                <?php endforeach;?>
                <?php wp_reset_query(); ?>
5
  • 2
    wp_reset_query is intended for use with query_posts, I recommend you purge both of them from your memory and use wp_reset_postdata instead. I'd also suggest using a standard post loop instead of a foreach, and, avoiding 'suppress_filters' => true, as it will prevent plugins and caching mechanisms from working
    – Tom J Nowell
    Oct 14, 2019 at 13:34
  • Alright thanks! Still I'm unable to run two queries in the same page Oct 14, 2019 at 13:38
  • Great advice @Tom and we don't care about your HTML. Get rid of it to ask.
    – Max Yudin
    Oct 14, 2019 at 13:40
  • I just added it, to give context to my question. Oct 14, 2019 at 13:41
  • There's nothing wrong with including the other HTML
    – Tom J Nowell
    Oct 14, 2019 at 14:12

1 Answer 1

1

Your problem is here:

foreach ($wqPort as $projeto)

$wqPort is an object of type WP_Query, not an array. If you look at your PHP error log, you should be seeing warnings and notices for those lines

Instead, use a standard post loop:

$q = new WP_Query( [ .. args here ... ] );
if ( $q->have_posts() ) {
    while( $q->have_posts() ){
        $q->the_post();
        // .. do things
    }
    wp_reset_postdata();
} else {
    // no posts found
}

That's what a standard WP_Query post loop looks like

Some additional notes:

  • Avoid setting suppress_filters to true, it will prevent caching mechanisms from working, as well as any plugins from adjusting the query, giving a performance penalty
  • Never use wp_rest_query, it's intended for use with query_posts. Use wp_reset_postdata instead
  • Don't smush multiple things on to the same line
  • Indent your code, it will prevent common errors and make it easier to read
  • If you have 2 PHP blocks with nothing in between them, don't close and reopen them, just have 1 large block. E.g. this: <?php echo 1; ?><?php echo 2;?> becomes: <?php echo 1; echo 2; ?>
2
  • Alright tom, thank you! I've used the while loop with the wp_query and the for each loop with the get_posts() method , since the get posts returns an array. the problem is i've used them in single category pages..now i'm using multiple queries in the home page to display multiple posts in diferent sections of the page. It's a little but confusing to explain.... I'm going to try again. Once again thanks for the help ! Oct 14, 2019 at 14:15
  • Keep in mind that get_posts uses WP_Query internally, eitherway, even if this doesn't fix things for you it was still a problem ( perhaps just one you didn't know you had ), there's nothing in WP that prevents using multiple WP_Query objects on the same page
    – Tom J Nowell
    Oct 14, 2019 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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