I have a page on my site where I need to do additional searching within that page. I am using the default way of WordPress searching with <?php get_search_form(); ?> for my main header search, which works great.

I created another HTML form on my page, not using the default PHP method, for the additional searching like so:

<form action="<?php bloginfo('siteurl'); ?>" id="searchform" method="get">
        <label for="s" class="screen-reader-text">Search for:</label>
        <input type="text" id="s" name="s" value="" />
        <input type="hidden" name="post_type" value="photo_group" /> 
        <input type="submit" value="Search" id="searchsubmit" />

I use the type=hidden for getting the custom post type that I want to search through:

<input type="hidden" name="post_type" value="photo_group" />

which gives me a link that looks similar to:



When I use the new search form created on the page, it uses the default search.php to display the results. How can I display the search results of the custom post type search, lets say in a div on the same page?

I've tried creating a new page template that would display the results with no success, could this be the right way of doing it and I just did it wrong?


I'm trying the solution of sanchothefat. Does this look right? This is my whole div for my search results and search query.

<div id="photo-search">
    <h2>Search Photos:</h2>
    <form action="<?php the_permalink(); ?>" id="searchform" method="get">
            <label for="s" class="screen-reader-text">Search for:</label>
            <input type="text" id="search" name="search" value="" />
            <input type="hidden" name="post_type" value="photo_group" /> 
            <input type="submit" value="Search" id="searchsubmit" />

    <?php if( isset( $_REQUEST['search'] ) ) {
        query_posts( array(
        's' => $_REQUEST['search'],
        'post_type' => $_REQUEST['photo_group'],
        'paged' => $paged

        if( have_posts() ) : while ( have_posts() ) :
        endwhile; endif;


I get 404 error page not found when running through the search.

  • Did my solution work for you in the end? Commented May 2, 2012 at 14:43
  • You pointed me in the right direction, so, in a way it did help me out. Thank you much.
    – Romes
    Commented May 2, 2012 at 15:29
  • 1
    what was your solution? I am trying to do the same thing - display the search results of a custom post in the same page as the search request and I keep getting a 404 page when I click submit
    – Claire
    Commented Nov 7, 2012 at 16:40
  • I was faced with the same solution and found that having post_type=my_cpt_post_type in the url was causing the problem. Much the same as changing the default input name from 's' to 'search' in the answer below I change my hidden input from 'post_type' to 'p_type' and that solved my problem. Then in your query you can use 'post_type' => $_REQUEST[ 'p_type' ]
    – philip
    Commented Feb 1, 2021 at 14:19

2 Answers 2


The simplest option if you want to show search results within a page context you'll need to do a custom loop, otherwise you won't be able to access the page information.

Change the input with the name s to something else like search or q to stop wordpress from doing it's usual built in search.

Next change the form action parameter to the current page's URL. You can use <?php get_permalink(); ?> for that.

The loop you need to do is as follows:

    if ( isset( $_REQUEST[ 'search' ] ) ) {
          // run search query
          query_posts( array(
             's' => $_REQUEST[ 'search' ],
             'post_type' => $_REQUEST[ 'post_type' ],
             'paged' => $paged

        // loop
        if ( have_posts() ) : while ( have_posts() ) :
            // loop through results here
        endwhile; endif;

        // return to original query
  • I have to change if ( have_posts() ) : while ( have_posts() ) : to if ( have_posts() ) : while ( have_posts() ) : the_post(); in order to avoid infinite loop (not sure why). Maybe because I'm rendering custom template inside with get_template_part(...).
    – jmarceli
    Commented Oct 26, 2015 at 12:23

You can modify the query before it gets processed:

add_action('parse_query', function($query){

  // not the search request

  // validate post type here (you should provide a white-list)
  $post_type = isset($_GET['post_type']) ? sanitize_key($_GET['post_type']) : false;

  // adjust the query
  if($post_type && post_type_exists($post_type))
    $query->set('post_type', $post_type);


This will get you posts matching the requested post type.

If you want to query multiple post types just pass an array instead: array('post', 'page', $post_type). And to group posts based on the type, check the type within the loop, and store your CPTs inside a temporary array, which you iterate after the normal posts have been displayed.

  • Is this something I put in my functions.php file or simply directly on the page?
    – Romes
    Commented Apr 24, 2012 at 14:02
  • yes, either that or a build a new plugin and paste the code into it Commented Apr 24, 2012 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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