最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Running multiple WP_Query

programmeradmin2浏览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(); ?>

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(); ?>
Share Improve this question asked Oct 14, 2019 at 13:27 Pedro FerreiraPedro Ferreira 1791 silver badge10 bronze badges 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 Commented Oct 14, 2019 at 13:34
  • Alright thanks! Still I'm unable to run two queries in the same page – Pedro Ferreira Commented 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 Commented Oct 14, 2019 at 13:40
  • I just added it, to give context to my question. – Pedro Ferreira Commented Oct 14, 2019 at 13:41
  • There's nothing wrong with including the other HTML – Tom J Nowell Commented Oct 14, 2019 at 14:12
Add a comment  | 

1 Answer 1

Reset to default 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; ?>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论