You’re right. It isn’t a good structure.
- You are running three queries on the page– the two you are creating
plus the main query that is being completely ignored (plus ancillary
queries). - You are also clobbering the main query halfway through the page load
when you overwrite$wp_query
, which can cause unexpected and
unpredictable results and difficulty with pagination. - You are overwriting other Core variables, like
$posts
- You have PHP tag SPAM
- Your code formatting in general is lacking, though I’ve seen much
worse.
What you want to do is alter the main query to exclude that post format:
function exclude_asides_wpse_194888($qry) {
if (is_tag()) {
$taxq = array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-aside'
),
'operator' => '!='
)
)
$qry->set('tax_query',$taxq);
}
}
add_action('pre_get_posts','exclude_asides_wpse_194888');
Then run your “asides” query:
$asides = new WP_Query(
array(
'posts_per_page' => 4,
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-aside'
),
)
)
)
);
if ( $asides->have_posts() ) {
while ( $asides->have_posts() ) {
$asides->the_post(); ?>
<div class="col-sm-3 col-xs-6">
<?php get_template_part( 'content', get_post_format() ); ?>
</div><?php
}
}
Then let the main query as normal:
if ( have_posts() ) {
while ( have_posts() ) {
$query->the_post();
if($i%4==0) { ?>
<div class="row"><?php
} ?>
<div class="col-sm-3 col-xs-6"><?php
get_template_part( 'content', get_post_format() ); ?>
</div><?php
$i++;
if($i%4==0) { // if counter is multiple of 3, put an closing div ?>
</div><?php
}
}
if($i%4!=0) { // put closing div here if loop is not exactly a multiple of 3 ?>
</div><?php
} ?>
<div class="blog">
<div class="row">
<div class="col-md-12"><?php
posts_nav_link(); ?>
</div>
</div>
</div>
}