Title: limit to posts
Last modified: September 1, 2016

---

# limit to posts

 *  [mncain](https://wordpress.org/support/users/mncain/)
 * (@mncain)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/limit-to-posts/)
 * I have tried limited the search with
 *     ```
       function SearchFilter($query) {
       if ($query->is_search) {
       $query->set('post_type', 'post');
       }
       return $query;
       }
   
       add_filter('pre_get_posts','SearchFilter');
       ```
   
 * this though also limits the search function all around.
 * Is is possible to to limited this search plug in to just the post?
 * [https://wordpress.org/plugins/bop-search-box-item-type-for-nav-menus/](https://wordpress.org/plugins/bop-search-box-item-type-for-nav-menus/)

Viewing 1 replies (of 1 total)

 *  Plugin Author [joe_bopper](https://wordpress.org/support/users/joe_bopper/)
 * (@joe_bopper)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/limit-to-posts/#post-7563960)
 * 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](https://codex.wordpress.org/WordPress_Query_Vars)(
   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](https://developer.wordpress.org/reference/functions/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

Viewing 1 replies (of 1 total)

The topic ‘limit to posts’ is closed to new replies.

 * ![](https://ps.w.org/bop-search-box-item-type-for-nav-menus/assets/icon-128x128.
   png?rev=1067133)
 * [Bop Search Box Item Type For Nav Menus](https://wordpress.org/plugins/bop-search-box-item-type-for-nav-menus/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/bop-search-box-item-type-for-nav-menus/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/bop-search-box-item-type-for-nav-menus/)
 * [Active Topics](https://wordpress.org/support/plugin/bop-search-box-item-type-for-nav-menus/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/bop-search-box-item-type-for-nav-menus/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/bop-search-box-item-type-for-nav-menus/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [joe_bopper](https://wordpress.org/support/users/joe_bopper/)
 * Last activity: [9 years, 10 months ago](https://wordpress.org/support/topic/limit-to-posts/#post-7563960)
 * Status: not resolved