Skip to content

Instantly share code, notes, and snippets.

@thecodepoetry
Created January 28, 2021 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thecodepoetry/fd5b08cff491967a8884f30353747bc4 to your computer and use it in GitHub Desktop.
Save thecodepoetry/fd5b08cff491967a8884f30353747bc4 to your computer and use it in GitHub Desktop.
Custom search for shortcode only for post
function cpsearchform( $form ) {
$form = '<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >
<div><label class="screen-reader-text" for="s">' . __('Search for:') . '</label>
<input type="text" value="' . get_search_query() . '" name="s" id="s" />
<input type="hidden" name="post_type" value="post" />
<input type="submit" id="searchsubmit" class="dt-btn" value="'. esc_attr__('Search') .'" />
</div>
</form>';
return $form;
}
add_shortcode('cpsearch', 'cpsearchform');
@thecodepoetry
Copy link
Author

Add the code in your child theme functions.php to create a custom search form only for posts and then use the shortcode [cpsearch] to display the form

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment