1

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="<?php the_permalink(); ?>">
 <!-- Query -->
<label>
<span class="screen-reader-text"><?php echo _x( 'Enter Keyword', 'label' )?></span>
<input type="search" class="search-field"
       placeholder="Keywords"
       value="<?= $q ?>"
       name="q"
       title="<?php echo esc_attr_x( 'Enter Keyword', 'label' ) ?>"/>
</label>
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' );
0

I don't think this is doing what you think it's doing, but there's a trivial solution

Use The s parameter with a pre_get_posts filter

Using s as the name of your input will allow you to do a direct search via the main query, speeding up the page load.

Additionally, you can then modify the search using the pre_get_posts filter to add the other parameters you want, e.g.

add_filter( 'pre_get_posts', function( \WP_Query $q ) {
    if ( !$q->is_main_query() || !$q->is_search() ) {
        return;
    }
    $q->set( 'posts_per_page', 2 );
    // etc...
} );

But I Don't Want To Use search.php, I A Different Template For This Particular Thing

That would be another question you should ask separately, but luckily, somebody beat you to it:

How to create a custom search for custom post type?

  • Thanks for the input. I solved my own issue with a simple check. $q should not have been assigned to get_query_var() on page load. I've added a simple conditional to check if we a keyword has been entered. ` if (isset($_GET['q'])) { $q = get_query_var( 'q' ); } else{ $q = null; } ` – ste Apr 24 '17 at 13:11
  • You should post that as an answer – Tom J Nowell Apr 24 '17 at 13:48
  • Sure, I'll post it now. Not sure if it's the best convention but it solved our issue here. This logic will be used for multiple archives like this so we shall see how it fairs Thanks Tom! – ste Apr 24 '17 at 14:10
0

Solved it with a simple conditional prior to setting up the query

if (isset($_GET['q'])) {
    $q = get_query_var( 'q' );
} else{
    // otherwise null, nothing to see here
    $q = null;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.