First off, as documented:
is_archive()
does not accept any parameters. If you want to check if
this is the archive of a custom post type, useis_post_type_archive( $post_type )
So in your code, use is_post_type_archive( 'movies' )
instead and not is_archive('movies')
.
Now as for the main question:
the main Query is not modified, the CPT showed are always the same
-
I hope you actually understand what the “main query” is, which basically an instance of the
WP_Query
class and the instance is accessible via the global$wp_query
variable. WordPress runs the main query on page load, before the page template is loaded, so the main query determines what template should be used. So for example, if you’re atexample.com/movies
(a post type archive page), then WordPress loads thearchive-movies.php
template, if it exists. -
I believe the main query is actually being modified, but you never displayed the posts in that query and instead, you created a custom query and then displayed the posts in that query (the
while ( $query->have_posts() ) ...
part), which returned the same posts regardless of the filters in your search form.And why so, is because the
$query->is_main_query()
check (which is good) in your function causes the search filters to be applied only on the main query, i.e. when the$query
is the global$wp_query
variable. But then, in your template, the$query
is not the main query and instead it’s a secondary/custom instance ofWP_Query
.
Therefore, you should instead use the main loop like so: (this is a super simple example; see The Loop in the theme developer handbook for more details about the loop)
// In your archive-movies.php file:
while ( have_posts() ) {
the_post();
the_title( '<h2>', '</h2>' );
}
And remove your custom query/loop (including the wp_reset_postdata();
part).
And keep in mind that the main query already retrieved the correct post(s) from the database, hence you should just display those posts, but if for example on an archive page you want to change the number of posts per page, and/or to include posts from certain categories only, then use the pre_get_posts
hook to alter the main query (arguments) instead of re-querying the database via a new WP_Query
instance.