Pagination URLs for taxonomies empty error 404?

I’ve created a custom WP_query for taxonomy pages to return attachments and posts. I then loop through attachments to get their parent ids and create an array of ids ($found_ids) for the below. They load a CPT ‘locations’.

However the paginated pages aren’t loading - I get 404. They seem fine for instance on the search page but not here even with the same CPT whose pagination works in search.

url: /location_features/white-kitchen/
body classes : archive tax-location_features term-white-kitchen term-67 logged-in app-data index-data archive-data taxonomy-data taxonomy-location_features-data taxonomy-location_features-white-kitchen-data

The loop works fine and pagination is showing. But the page links lead to a dead end?

url: /location_features/white-kitchen/page/2/ 
body classes : error404 logged-in app-data index-data 404-data

wp_query with pagination. All works and if a set $paged to 2 it loads the next pages posts.

    public function taxLocationTypes(){
     // ... previous code grabs builds the  $found_ids array.
    $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
    $matched_parent_args = [
        'post_type'      => ['location'],
        'post__in'       => $found_ids,
        'posts_per_page' => 3,
        'paged'          => $paged,

    $matched_locations = new \WP_Query($matched_parent_args);

    return $matched_locations;

Two loops on this page. Standard and $matched_locations. Fine if no pagination in the url.

Neither load anything if go to /page/2/. If I add /page/1/ /page/1/. gets removed and shows the posts.

{{-- cutom loop --}}
@while ($tax_location_types->have_posts()) 
@php $tax_location_types->the_post() @endphp

$GLOBALS['wp_query']->max_num_pages = $tax_location_types->max_num_pages;
the_posts_pagination( array(
   'mid_size' => 1,
   'prev_text' => __( 'Back', 'green' ),
   'next_text' => __( 'Onward', 'green' ),
   'screen_reader_text' => __( 'Posts navigation' )
) );  

 wp_reset_postdata() @endphp
  @while (have_posts()) @php the_post() @endphp
  {!! get_the_posts_navigation() !!}

Can anyone see what is wrong or what issue this might be? Thanks in advance.

I think there’s an issue with this. Using $GLOBALS['wp_query'] seems like a bit of hack.

This topic was automatically closed after 42 days. New replies are no longer allowed.