Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The outlined solutions are poor. Editing the core impedes on upgradeability of the WordPress install - you'll have to repeat that change every time you update, which you should be doing. The other solution places unnecessary load on the database by filtering results <em>after</em> retrieving them. The better solution:</p> <p>In your theme's functions.php, add a new function to write out a search form:</p> <pre><code>function custom_search_form( $form, $value = "Search", $post_type = 'post' ) { $form_value = (isset($value)) ? $value : attribute_escape(apply_filters('the_search_query', get_search_query())); $form = '&lt;form method="get" id="searchform" action="' . get_option('home') . '/" &gt; &lt;div&gt; &lt;input type="hidden" name="post_type" value="'.$post_type.'" /&gt; &lt;input type="text" value="' . $form_value . '" name="s" id="s" /&gt; &lt;input type="submit" id="searchsubmit" value="'.attribute_escape(__('Search')).'" /&gt; &lt;/div&gt; &lt;/form&gt;'; return $form; } </code></pre> <p>Now, in the template where you want the form (or within any widgets you've created, this could easily be registered as a widget instead), this:</p> <pre><code>&lt;?= custom_search_form( null, 'Search posts', 'post'); ?&gt; </code></pre> <p>The arguments could be left out from the function &amp; call, but I find them helpful. The key to this whole thing is the hidden input 'post_type', which passes the value to the query. The default, <code>post</code>, will ensure only posts are returned.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload