WordPress Tricks

WordPress Blog Post Search Form Code: Only Post Search and Exclude pages and others

If you need to put a search form to show blog post based on the search term, you can use the code I shared. Generally normal search form may show you result from pages and other custom post type. But here I used the form which is used to show only blog posts.

wordpress blog post search form

Here is the code:

** based on the theme you are using, the css code i shared may need to be modified. Please do comments if you face any issue.

<style>
			input#search {
    border: 2px solid lightgrey;
    padding: 5px;
    border-radius: 10px;
  padding-left: 12px !important;
}
			form {
    float: right;
}
			input[type="image"] {
    top: 15px;
    display: inline-block;
    position: relative;
}
			input[type="image"] {
    top: 4px;
    display: inline-block;
    position: relative;
    left: -35px;
    width: 23px;
}
		</style>	
		<form action="/" method="get">
     
    <input type="text" name="s" id="search" value="<?php the_search_query(); ?>" placeholder="Search" />
    <input type="image" alt="Search" src="https://allaboutbasic.files.wordpress.com/2021/08/search-2.png" />
			<input type="hidden" name="post_type" value="post" />
</form>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s