I'm trying to create a search page for a custom post type. This is the code I'm using:

$s = $_GET['s'];
$args = array(
        'post_type' => 'companies',
        's'         => $s

if ( have_posts() ) :

The problem is that adding the "s" parameter in query_posts() gives me zero results no matter what the "s" value is. It seems like the whole search mechanism is not working at all. Any suggestions?


You shouldn't use $_GET['s'] directly like that, and you definitely shouldn't use query_posts, although I understand using it for testing.

If you're going to build your own search mechanism in this way, then you should run $_GET['s']through the urldecode() function first, to make sure it doesn't contain weird data that you might not expect and which could muck up your search.

Realistically, just adding the post_type to the search form will make it search that post type automatically, you don't need to do your own query_posts call. The only thing left to do is to make sure you have the post_type correct (it is "companies", right?) and to make sure that there actually is content containing whatever you're searching for.

Searching works fine with custom post types. There's nothing special or specific that needs to be done to make it work.

Make sure that the post type is a public type, or doesn't have the "exclude_from_search" flag set for it.


First avoid query_post it is horrendously bad practice, and never grab 's' via GET. Read this before continuing:


It's a set fo slides for a presentation that will teach you the correct way to make and modify queries in WordPress, and it's written by a Core developer. It should be considered essential reading for anyone working with WordPress and it will save you time and money.

Back to the search issue. A URL such as this will do what you desire:


So the problem is how to replicate it in a search form. Your search form should be something like:

<form action="<?php home_url(); ?>" method="get">
    <input type="text" name="s" /><?php /* your search term */ ?>
    <input type="hidden" name="post_type" value="companies" /><?php /* set post type */ ?>
    <input type="submit" /><?php /* submit button */ ?>

You should make no modifications to search.php as WordPress will handle that part for you internally.

You should also verify you have posts of type 'companies' to search, and that the search term is there. Devise a test, where you search for X and post Y shows up, and you know for a fact that Y has that search term.


The best way I've found to search custom post types is add an input field like this to your search form:

<input type="hidden" name="post_type" value="post-type-name" />

This input can be a radio button as well, to select different post types. Just make sure the name that's passed matches the registered post type string. Leave the value blank in radio buttons for 'search all'.

This will be added to the URL as a query string, and will be processed by WordPress on the search results page without any extra work on your part.

In other words - you shouldn't need to use query_posts() at all. In fact, the commonly held opinion of many developers here is that you should never use query_posts() for anything, as there's better and more efficient ways of doing everything it does.

  • that was the first thing I tried to do but it didn't work either... this issue is causing me headache – ktsixit Jun 30 '12 at 8:59
  • And you're doing this all in search.php, right? – SickHippie Jul 2 '12 at 16:16
  • My form is placed in sidebar.php. Only results are displayed in search.php. – ktsixit Jul 4 '12 at 12:57

Maybe, in header.php file you use query_post() function. That function will be clear all request string, that cause your search do not working.

You should change the function to get post.

solved here: http://thinhseven.com/wordpress-search-do-not-working/

  • 1
    Great to have you here at WPSE. It's better to post the relevant code bits here as links might be removed in the future. This then renders an answer useless without context. It's not an issue to quote a solution and cite it with a link. – Brian Fegter Oct 3 '12 at 11:15
  • Just as Brain said, link is dead. – marek.m Sep 14 '17 at 12:59

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.