Hi mncain,
Sorry for the large delay in response. In order to be able to limit a search that came only through the search box from this plugin, you will need to add a bit of distinguishing data to the form.
At the moment the data sent to the server is simply ?s=your_search_term
– as appears in your url. Hence you need to add something like &is_from_menu=yes
to make ?s=your_search_term&is_from_menu=yes
.
To achieve this, add:
add_action( 'bop_nav_search_hidden_inputs', function(){
?>
<input type="hidden" name="is_from_menu" value="yes">
<?php
} );
to the functions.php (or equivalent) file.
Now the get request data is sent to the server, but the server isn’t doing anything with it. If you add:
add_filter( 'query_vars', function( $qvars ) {
$qvars[] = 'is_from_menu';
return $qvars;
} );
then WordPress will recognise this is_from_menu
‘s existence as a publicly queryable variable (i.e. can adjust the output for any normal site user).
Finally, we get to the stage you were at:
add_filter('pre_get_posts', function($query) {
if ($query->is_search && get_query_var( 'is_from_menu', 'no' ) == 'yes' ) {
$query->set('post_type', 'post');
}
return $query;
} );
The get_query_var function fetches the data that WP now recognises and then we check if it is ‘yes’. If so, set the post_type as post.
Note that if all you want to do is set post_type as post, you can miss a lot of the previous code as post_type
is already a publicly queryable variable. So, you could just use:
add_action( 'bop_nav_search_hidden_inputs', function(){
?>
<input type="hidden" name="post_type" value="post">
<?php
} );
and that should work.
Hope this helps.
Cheers,
Joe