Hi there, I have a custom post type archive that I’m using pre\_get\_posts on and I also would like to add form to search them by title. Issuing being is that when I perform a search with the form I see all posts instead of the of expected results. What I have noticed is on a page that I DO have it working, query monitor catches the ‘s’ parameter, but on this particular page it doesn’t. Any ideas?
archive-researcher.php
<?php get_header(); ?>
<div id=”site-content” class=”content-area”>
<main id=”main” class=”site-main”>
<section class=”hero” style=”background:url(‘<?php if (has_post_thumbnail()) : the_post_thumbnail_url() ?>’)
<?php else : echo get_stylesheet_directory_uri(); ?>/image/default-hero.jpg’) no-repeat center center/cover;
<?php endif; ?>”>
<div class=”container d-flex flex-column align-items-center position-relative”>
<article>
<div class=”hero-content”>
<h1 class=”text-center”>
Contact Researchers
</h1>
</div>
</article>
<img class=”overlay” src=”<?php echo get_stylesheet_directory_uri()?>/image/overlay.svg” />
</div>
</section>
<section class=”my-4 section”>
<div class=”container”>
<form method=”GET” action=”<?php echo get_post_type_archive_link(“researcher”); ?>”>
<div>
<input type=”text” name=”r_name” id=”r_name” placeholder=”Search”
value=”<?php echo get_query_var(“r_name”, false); ?>”>
</div>
<div>
<button>Search</button>
<a href=”<?php echo get_post_type_archive_link(“researcher”); ?>”>Reset</a>
</div>
</form>
</div>
</section>
<section>
<div class=”container”>
<div class=”row”>
<?php if (have_posts()): ?>
<?php while (have_posts()): the_post(); ?>
<div class=”col-12 col-lg-4 d-flex”>
<?php get_template_part(‘template/card’, ‘researcher’); ?>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<?php else: ?>
<div class=”col-12″>
<p>No researchers found.</p>
</div>
<?php endif; ?>
</div>
<?php understrap_pagination(); ?>
</div>
</section>
</main>
</div>
<?php get_footer(); ?>
functions.php
function add_query_vars_filter($vars)
{
// add custom query vars that will be public
// https://codex.wordpress.org/WordPress_Query_Vars
//researchers
$vars[] .=”r_name”;
//resources
$vars[] .= “r_title”;
$vars[] .= “type”;
$vars[] .= “topic”;
$vars[] .= “crop”;
$vars[] .= “research-area”;
$vars[] .= “resource-author”;
$vars[] .= “resource-year”;
return $vars;
}
add_filter(“query_vars”, “add_query_vars_filter”);
function researcher_archive($query)
{
// only run this query if we’re on the researcher archive page and not on the admin side
if (
$query->is_archive(“researcher”) &&
$query->is_main_query() &&
!is_admin()
) {
// get query vars from url.
// https://codex.wordpress.org/Function_Reference/get_query_var#Examples
// example.com/researcher/?r_name=steve
$name = get_query_var(“r_name”, false);
// Add search parameter for post title
$query->set(“s”, $name);
$query->set(‘posts_per_page’, 12);
$query->set(‘orderby’, ‘wpse_last_word’);
$query->set(‘order’, ‘ASC’);
}
}
add_action(“pre_get_posts”, “researcher_archive”);

Maybe change this
`$query->is_archive(“researcher”)`
To this
`$query->is_post_type_archive(“researcher”)`
Not sure if it will make a difference: [is_archive](https://developer.wordpress.org/reference/classes/wp_query/is_archive/) does not take an argument, but [is_post_type_archive](https://developer.wordpress.org/reference/classes/WP_Query/is_post_type_archive) does.
Another approach entirely is to write the entire code in the archive-researcher.php template file and use a custom wp query instead of hooking into the main query in the functions.php file.
<section>
<div class=”container”>
<div class=”row”>
<?php
$args = array(
‘post_type’ => ‘researcher’,
‘posts_per_page’ => 12,
‘orderby’ => ‘wpse_last_word’,
‘order’ => ‘ASC’,
);
if (!empty(get_query_var(‘s’))) {
$args[‘s’] = get_query_var(‘s’);
}
$researcher_query = new WP_Query($args);
if ($researcher_query->have_posts()) :
while ($researcher_query->have_posts()) : $researcher_query->the_post(); ?>
<div class=”col-12 col-lg-4 d-flex”>
<?php get_template_part(‘template/card’, ‘researcher’); ?>
</div>
<?php endwhile;
wp_reset_postdata();
else : ?>
<div class=”col-12″>
<p>No researchers found.</p>
</div>
<?php endif; ?>
</div>
<?php
// Pagination
the_posts_pagination(array(
‘total’ => $researcher_query->max_num_pages,
));
?>
</div>
</section>