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
|
1 Answer
Reset to default 1Your 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 withquery_posts
. Usewp_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; ?>
wp_reset_query
is intended for use withquery_posts
, I recommend you purge both of them from your memory and usewp_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