query_vars overriding WP_Query args

Looking for some fresh eyes on this,

I have a custom archive for my CPT and it’s Taxonomies.

The page has a input search form to filter posts of the CPT as well as drop downs to filter by terms.

The issue is that on page load, when the loop instantiates WP_Query args are overridden by get_query_vars.

For example, the posts_per_page is set to 2.
However, query_vars hijacks the args and changes it to 12 (for some reason, not declared anywhere else on the site)

Logic below.

$q = get_query_var( 'q' );

$args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC',
'paged' => $paged,
'posts_per_page' => $posts_per_page, // tried 2
'tax_query' => $tax_query,
's' => $q
);
$wp_query = new WP_Query( $args );

Form – see q as value

<form id="filter-form" role="search" method="get" class="" action="">


etc

The filter is registered and hooked in functions.php as per codex

function add_query_vars_filter( $vars ){
$vars[] = "q";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );

Read more here: query_vars overriding WP_Query args

Leave a Reply

Your email address will not be published. Required fields are marked *