I've created a custom search for my custom posts, using the accepted answer from Creating a search form for custom fields.

However, I'm struggling with the resulting page - I need to conditionally enqueue some javascript, but the results page fails every conditional I throw at it. I eventually installed the plugin "What Template Am I Using", and it too, says the following:

Post Type: not set

Front: No

Home: No

404: No

Search: No

and the "template" section is blank (which is kind of what I found - various get_the_template() functions are come back blank.

What's going on? And is there another way I can conditionally enqueue my javascript?

[EDIT]: The conditionals I used are:

  • if( is_search() ) {echo "<h1>Foo Search</h1>"; }
  • if( is_page_template( 'content-search.php' ) ) {echo "<h1>Bar Template</h1>"; }
  • if( is_page_template( 'search-advanced.php' ) ) {echo "<h1>FooBar Template</h1>"; }
  • if( is_archive( ) ) {echo "<h1>Foo Archive</h1>"; }

None of the above evaluated as true. I tried these both in the search results page, just before the loop, and in functions.php.

Here is my search form: http://pastebin.com/Cq177zWV

And here is my search results page (search-advanced.php): http://pastebin.com/FxFpmECH

And here is the snippet from my functions.php (via Creating a search form for custom fields.): function shr_load_custom_search_template(){ if( isset( $_REQUEST['search'] ) && $_REQUEST['search'] == 'advanced' && ! is_admin() ) { require('search-advanced.php'); die(); } } add_action('init','shr_load_custom_search_template');

  • Can you post the relevant parts of your code? Mainly the search form markup. You should be able to use is_search() here. – jdm2112 Nov 18 '16 at 16:47
  • The search results are typically shown via search.php in your theme. What conditionals did you throw at the enqueue script? (agree with @jdm2112 that is_search() typically should work). Where did you put those conditionals? – Michelle Nov 18 '16 at 17:48
  • 1
    The is_ conditionals are set after the query runs, on the wp action. You are halting execution on the init action, before the main query runs, which is way too early. Your search should hook pre_get_posts to modify the main query rather than generating a new query in a template file. – Milo Nov 21 '16 at 16:34
  • Thanks @Milo - will try that first thing in the morning! – gillespieza Nov 22 '16 at 20:31
  • I found the problem - I wasn't actually submitting the value "s" (I had changed the standard <input type=submit> to a button instead. As soon as I added a hidden input with value "s", is_search() started evaluating true. – gillespieza Nov 23 '16 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.