The problem is not that you never specify which event cat in your query, but that you’re doing a query at all.
If you want to change what the main query pulls in, change the query, don’t add a second query. We do this via the pre_get_posts
filter:
This hook is called after the query variable object is created, but before the actual query is run.
The pre_get_posts action gives developers access to the $query object by reference (any changes you make to $query are made directly to the original object – no return value is necessary).
For example:
function my_home_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '123' );
}
}
add_action( 'pre_get_posts', 'my_home_category' );
In your case, you want to use the filter:
- only if it’s the main query
- only if it’s an archive
- and that archive is for the events category
- to set the post status to
future
notpublish
- and change the ordering from
DESC
TOASC
Since the current requested term etc are already set, you will need a check similar to this:
function randomer11_fix_eventcat_archive( \WP_Query $q ) {
if ( $q->is_main_query() && $q->is_archive() && $q->is_tax( 'eventcat' ) ) {
$q->set( ... ); // set your options
}
}
add_action( 'pre_get_posts', 'randomer11_fix_eventcat_archive' );
You can call set
as many times as you want, and now in your template, you can remove your query, and use a normal post loop. Pagination will work how its supposed to without any hacks, and your page will load a lot faster