Trying to change category of wp_query
I'm trying to change the category of the main loop, after printing posts from each category. The page shows:
- 3 most recent posts
- 3 posts from first category
- 3 posts from second category
- 3 posts from third category
- 3 posts from fourth category
The category-specific posts can't be from the most recent.
The category-specific posts aren't right, though. I see posts, but they're not from the right category!
Here's my template. It uses blade, so slightly different syntax, but more or less the same as PHP.
@php
$args = array(
'numberposts' = 3,
'fields' = 'ids'
);
$posts = new WP_Query( $args );
@endphp
@include ('partials.content-lh-post-previews', ['featured_text' = true, 'limit' = 3, 'headline' = 'Recent Articles', 'include_cat' = true])
{{-- Posts by category --}}
@php
$args = array(
'taxonomy' = 'category',
'parent' = 0,
'fields' = 'ids',
'meta_key' = 'category_weight',
'orderby' = 'meta_value_num'
);
$terms = get_terms($args);
@endphp
@foreach ($terms as $index = $term)
@php wp_reset_postdata(); @endphp
@php
$posts-query['cat'] = [$term];
$term_name = get_term( $term )-name;
$headline = $index == 0 ? 'Featured Stories' : '';
@endphp
@include ('partials.content-lh-post-previews', ['featured_text' = false, 'limit' = 3, 'supertext' = $term_name, 'view_more' = $term, 'headline' = $headline])
@endforeach
And the template partial I call on lines 9 and 34:
?php
$count = 0;
?
@if ($posts)
section class=post-preview-section
div class=wrapper
@if ( isset($headline) $headline )
h2 class=header header-large post-preview-section-headline{{ $headline }}/h2
@endif
@isset ( $supertext )
label class=post-preview-section-supertext supertext{{ $supertext }}/label
@endisset
div class=post-preview-container{{ isset($view_more) ? ' view-more' : '' }}
?php while ($posts-have_posts() $count $limit) : $posts-the_post(); ?
@php
$image = get_field('small_image');
$title = get_the_title();
$copy = $featured_text ? get_field('featured_text') : null; // only show post copy if is latest section
if ( isset($include_cat) $include_cat ) :
$cat = get_field('main_category')['label'];
endif;
@endphp
a class=post-preview href={{ get_the_permalink() }}
@if ( $image )
img src={{ $image }} class=post-preview-image
@endif
div class=post-preview-copy-area
@isset( $include_cat )
label class=post-preview-supertext?php echo $cat ?/label
@endisset
h3 class=header header--small post-preview-title{!! $title !!}/h3
@if ( $copy )
div class=post-preview-copy{!! $copy !!}/div
@endif
div class=arrow post-preview-linkRead the article/div
/div
/a
?php $count++; ?
?php endwhile; ?
@isset($view_more)
@php
$term = get_term($view_more);
$term_icon = get_field('category_icon', $term);
$term_color = get_field('category_color', $term);
$term_link = get_category_link($view_more);
$term_name = $term-name;
@endphp
a href={{ $term_link }} class=post-preview post-preview-more style=background-color: {{ $term_color }}
@if ( $term_icon )
div style=background-color: {{ $term_color }} class=post-preview-more-icon-container post-preview-image
img src={{ $term_icon['url'] }} class=post-preview-more-icon alt={{ $term_icon['alt'] }}
/div
@endif
div class=post-preview-copy-area
h3 class=post-preview-title header header--smallMore {{ $term_name }} Articles/h3
div class=arrow post-preview-linkView all/div
/div
/a
@endisset
/div
/div
/section
@endif
@php wp_reset_postdata() @endphp
Why isn't the category working?