Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Difference between revisions of "Creating a Search Page"

m (capital P dangit)
(Preserving Search Page Results and Pagination)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{Languages|
  +
{{en|Creating_a_Search_Page}}
  +
{{ja|検索ページの作成}}
  +
}}
  +
 
__TOC__
 
__TOC__
 
A Search Page is a WordPress [[Pages|Page]] with a custom [[Pages#Page_Templates|Page template]] to give users more information for searching your site.
 
A Search Page is a WordPress [[Pages|Page]] with a custom [[Pages#Page_Templates|Page template]] to give users more information for searching your site.
Line 4: Line 9:
 
== Things You Need to Know ==
 
== Things You Need to Know ==
   
Different WordPress Themes feature different [[Templates|template files]]. Some include a <tt>[[Template_Hierarchy#Search_Result_display|search.php]]</tt> template file. This is '''not''' a ''Search Page'', it is merely a template that displays the search results. There is also a template file called <tt>searchform.php</tt>. This is a template file that is often included in the sidebar of many themes and generates the search box form. If there isn't one in your theme, you can easily copy it from the Default theme.
+
Different WordPress Themes feature different [[Templates|template files]]. Some include a <tt>[[Template_Hierarchy##search-result|search.php]]</tt> template file. This is '''not''' a ''Search Page'', it is merely a template that displays the search results. There is also a template file called <tt>searchform.php</tt>. This is a template file that is often included in the sidebar of many themes and generates the search box form. If there isn't one in your theme, you can easily copy it from the Default theme.
   
 
To create your own custom Search Page, you will need to create a [[Pages#Page_Templates|Page template]] to include your search form and the information you want your users to see before they search your site.
 
To create your own custom Search Page, you will need to create a [[Pages#Page_Templates|Page template]] to include your search form and the information you want your users to see before they search your site.
Line 10: Line 15:
 
Check your WordPress Theme to see if it includes a <tt>page.php</tt> template file. The Default WordPress Theme does include this template, but many do not. If it does, then follow [[#Using the page.php|these instructions]]. If it does not, we have the information you need to [[#Creating a Search Page Template|create your own]].
 
Check your WordPress Theme to see if it includes a <tt>page.php</tt> template file. The Default WordPress Theme does include this template, but many do not. If it does, then follow [[#Using the page.php|these instructions]]. If it does not, we have the information you need to [[#Creating a Search Page Template|create your own]].
   
== Using the page.php ==
+
== Creating a Search Page Template ==
 
1. Using a [[Glossary#Text editor|text editor]], open the <tt>page.php</tt> and save as <tt>searchpage.php</tt>. ('''Note:''' The filename <tt>search.php</tt> is reserved as a special template name, so avoid its usage; the suggested <tt>searchpage.php</tt> just makes it easy to recognize in the list of files.)
 
   
  +
1. Using a [[Glossary#Text editor|text editor]], open the <tt>page.php</tt> and save as <tt>searchpage.php</tt>. If you do not have a <tt>page.php</tt>, you can create one based upon your Theme's <tt>index.php</tt> template file.<br />
  +
'''Note:''' The filename <tt>search.php</tt> is reserved as a special template name, so avoid its usage; the suggested <tt>searchpage.php</tt> just makes it easy to recognize in the list of files.<br />
 
2. After saving it, edit the file:
 
2. After saving it, edit the file:
 
* Delete [[The Loop]] (i.e. basically everything within your content <tt>div</tt>), leaving the <tt>div</tt> tags intact.
 
* Delete [[The Loop]] (i.e. basically everything within your content <tt>div</tt>), leaving the <tt>div</tt> tags intact.
Line 21: Line 26:
 
<pre><?php get_search_form(); ?></pre>
 
<pre><?php get_search_form(); ?></pre>
   
* At the top of your <tt>searchpage.php</tt>, before anything else, add this to give your Search Page a heading WordPress will recognize in the Administration Panels:
+
* At the top of your <tt>searchpage.php</tt>, before anything else, add this to give your Search Page a heading WordPress will recognize in the Administration Screens:
   
 
<pre><?php
 
<pre><?php
Line 28: Line 33:
 
*/
 
*/
 
?></pre>
 
?></pre>
* Save the file
 
* Upload the file to your theme directory (if you made changes to your <tt>style.css</tt> style sheet file, upload that, too).
 
   
  +
3. Save the file.<br />
<strong>For certain older versions of WordPress</strong>
 
  +
4. Upload the file to your theme directory (if you made changes to your <tt>style.css</tt> style sheet file, upload that, too).
* In the [[Administration Panels]] go to [[Administration_Panels#Write_-_Make_some_content|Write]] > [[Write Page SubPanel|Write Page]].[[Image:writepage2_5.png|thumb|right|WordPress Write Page Panel]]
 
* In the title field enter '''Search'''.
 
* '''Do not write anything in the content area.'''
 
* From the [[Write_Page_SubPanel#Page Template|Page Template]] drop-down menu select: '''Search Page'''
 
* Click the '''Create New Page''' button
 
* Select the [[Administration_Panels#Pages|Manage Pages]] panel.
 
* Write down the <tt>page_id</tt> number of your newly created [[Pages|Page]] named '''Search Page'''.
 
   
  +
If you create <tt>searchpage.php</tt> from page.php in Twenty Seventeen, it would be as like as followings:
<strong>For WordPress v3.0 and above</strong>
 
* In the [[Administration Panels]] go to Pages > Add New.
 
* In the title field enter '''Search'''.
 
* '''Do not write anything in the content area.'''
 
* While still on the same page, look for ''Page Attributes''.
 
* Select the drop-down menu in ''Template'', and select ''Search Page''.
 
* Click the '''Publish''' button.
 
* Write down the <tt>page_id</tt> number of your newly created [[Pages|Page]] named '''Search''' so you can link to it in future.
 
 
You are now ready to customize the look of your Search Page.
 
 
== Creating a Search Page Template ==
 
 
If you do not have a <tt>page.php</tt>, you can create one based upon your Theme's <tt>index.php</tt> template file.
 
 
# In a [[Glossary#Text editor|text editor]], open your theme's <tt>index.php</tt> and save as <tt>searchpage.php</tt>.
 
# Then follow the editing steps from [[#Using the page.php|above]].
 
 
== Preserving Search Page Results and Pagination ==
 
 
Search results and Pagination may stop working when applying customization to the search template. To avoid these issues the first thing any developer needs to do is add the following code to the start of their Search template to ensure that the original WordPress query is preserved. To customize the query append additional arguments to <em>(array) $search_query</em>. Execute the $search_query through a new $wp_query object, more information on the WP_Query object can be found at https://codex.wordpress.org/Class_Reference/WP_Query
 
   
 
<pre>
 
<pre>
 
<?php
 
<?php
  +
/*
global $query_string;
 
  +
Template Name: Search Page
  +
*/
  +
?>
  +
<?php
  +
get_header(); ?>
   
  +
<div class="wrap">
$query_args = explode("&", $query_string);
 
  +
<div id="primary" class="content-area">
$search_query = array();
 
  +
<main id="main" class="site-main" role="main">
   
  +
<?php get_search_form(); ?>
foreach($query_args as $key => $string) {
 
$query_split = explode("=", $string);
 
$search_query[$query_split[0]] = urldecode($query_split[1]);
 
} // foreach
 
   
  +
</main>&lt;!-- #main -->
$search = new WP_Query($search_query);
 
  +
</div>&lt;!-- #primary -->
?>
 
  +
</div>&lt;!-- .wrap -->
  +
  +
<?php get_footer();
 
</pre>
 
</pre>
   
  +
== Creating a Search Page ==
Additional customization arguments can be found on the Codex at https://codex.wordpress.org/Class_Reference/WP_Query
 
   
  +
Based on the Search Page Template, we will create the seach page.
== Display Total Results ==
 
   
  +
# In the [[Administration Screens|Administration Screen]] go to Pages > Add New.
To access the total number of search results from a search, you should retrieve the total number of posts found using the wp_query object.
 
  +
# In the title field enter '''Search'''.<br />'''Do not write anything in the content area.'''
<pre><?php
 
  +
# While still on the same page, look for ''Page Attributes'' on right side menu.
global $wp_query;
 
  +
# Select the drop-down menu in ''Template'', and select ''Search Page''.
$total_results = $wp_query->found_posts;
 
  +
# Click the '''Publish''' button.
?>
 
</pre>
 
More information on WP Query can be found on the Codex at https://codex.wordpress.org/Class_Reference/WP_Query
 
   
  +
It will show simple search form such as
  +
<div style="border: 1px solid #000000;">
  +
[[Image:custom_search_page_1.jpg|600px]]
  +
</div>
 
== Linking to Your Search Page ==
 
== Linking to Your Search Page ==
   
 
You can now make a link to your custom Search Page in several ways.
 
You can now make a link to your custom Search Page in several ways.
   
  +
=== Using the Page ID ===
;Using the Page ID :Whether or not you use permalinks, you can link to your new Search Page by using Page ID number of the Page.
 
  +
Whether or not you use permalinks, you can link to your new Search Page by using Page ID number of the Page. Insert the next line into your any posts, pages or templates
<pre><a href="index.php?page_id=17" title="Search Page">Search Page</a>
 
   
  +
<pre><a href="index.php?page_id=17" title="Search Page">Search Page</a></pre>
OR
 
   
  +
OR you may insert the next line into templates
<a href="<?php bloginfo('url'); ?>/?page_id=17">Search Page</a>
 
</pre>
 
; Using the Page Slug :The Page slug is set in the [[Write_Page_SubPanel|Edit Page]] panel. It is the name of the page if you are using [[Using Permalinks|Permalinks]]. You can manually change this. An example of a Page slug link would be:
 
<pre><a href="/wordpress/search-page" title="Search Page">Search Page</a>
 
   
  +
<pre><a href="<?php echo home_url(); ?>/?page_id=17">Search Page</a></pre>
OR
 
   
  +
=== Using the Page Slug ===
<a href="<?php bloginfo('url'); ?>/wordpress/search-page" title="Search Page">Search Page</a></pre>
 
  +
The Page slug is set in the [[Write_Page_SubPanel|Edit Page]] screen. It is the name of the page if you are using [[Using Permalinks|Permalinks]]. You can manually change this. An example of a Page slug link would be:
;Using <tt>wp_list_pages()</tt> :If you are using the [[Template Tags/wp_list_pages|<tt>wp_list_pages()</tt>]] template tag, the page name would be automatically generated in your Pages list.
 
  +
<pre><a href="/wordpress/search-page/" title="Search Page">Search Page</a></pre>
  +
for any posts, pages or templates when slug is 'search-page'. OR you may insert the next line into templates
   
  +
<pre><a href="<?php echo home_url(); ?>/wordpress/search-page/" title="Search Page">Search Page</a></pre>
== Customizing Your Search Page ==
 
   
  +
=== Using <tt>wp_list_pages()</tt> ===
[[Image:managepage2_5.png|thumb|120px|right|Manage Pages Panel]]Now that you have created your custom Search Page, you can customize the display. From the [[Administration_Panels#Manage_-_Change_your_content|Manage]] > [[Administration_Panels#Pages|Pages]] panel, on your custom Search Page line, choose '''Edit'''. Or open your <tt>searchpage.php</tt> in a text editor and edit it there.
 
  +
If you are using the [[Template Tags/wp_list_pages|<tt>wp_list_pages()</tt>]] template tag, the page name would be automatically generated in your Pages list.
  +
  +
== Customizing Your Search Page ==
   
  +
Now that you have created your custom Search Page, you can customize the display. Open your <tt>searchpage.php</tt> in a text editor and edit it there.
Above the <tt>include</tt> function for your <tt>searchform.php</tt> within the content <tt>div</tt>, you can add text to help visitors search your site.
 
  +
Above the <tt>get_search_form()</tt> function for your <tt>searchform.php</tt> within the content <tt>div</tt>, you can add text to help visitors search your site.
   
 
<pre>
 
<pre>
 
<p>
 
<p>
 
My Site features articles about
 
My Site features articles about
<a title="WordPress Articles" href="index.php?cat=4">WordPress</a>,
+
<a title="WordPress Articles" href="/category/wordpress/">WordPress</a>,
<a title="Web Design Articles" href="index.php?cat=5">web page design</a>,
+
<a title="Web Design Articles" href="/category/web-design/">web page design</a>,
<a title="Development Articles" href="index.php?cat=6">website
+
<a title="Development Articles" href="/category/website-development/">website development</a>,
  +
and <a title="CSS Articles" href="/category/css/">CSS</a>.
development</a>,
 
and <a title="CSS Articles" href="index.php?cat=7">CSS</a>.
 
 
</p>
 
</p>
 
<p>To search my website, please use the form below.</p>
 
<p>To search my website, please use the form below.</p>
Line 128: Line 115:
 
<?php get_search_form(); ?>
 
<?php get_search_form(); ?>
 
</pre>
 
</pre>
  +
<div style="border: 1px solid #000000;">
 
  +
[[Image:custom_search_page_2.jpg|600px]]
  +
</div>
 
You might want to include a list of keywords or other information, images, or details to customize your custom Search Page.
 
You might want to include a list of keywords or other information, images, or details to customize your custom Search Page.
  +
  +
== Preserving Search Page Results and Pagination ==
  +
  +
Search results and Pagination may stop working when applying customization to the search template. To avoid these issues the first thing any developer needs to do is add the following code to the start of their Search template to ensure that the original WordPress query is preserved. To customize the query append additional arguments to <em>(array) $search_query</em>. Execute the $search_query through a new $wp_query object, more information on the WP_Query object can be found at [[Class_Reference/WP_Query|WP_Query]].
  +
  +
<pre>
  +
<?php
  +
  +
global $query_string;
  +
  +
wp_parse_str( $query_string, $search_query );
  +
$search = new WP_Query( $search_query );
  +
  +
?>
  +
</pre>
  +
  +
Additional customization arguments can be found at [[Class_Reference/WP_Query|WP_Query]].
  +
  +
== Display Total Results ==
  +
  +
To access the total number of search results from <tt>search.php</tt>, a search result page, you should retrieve the total number of posts found using the wp_query object.
  +
<pre><?php
  +
global $wp_query;
  +
$total_results = $wp_query->found_posts;
  +
?>
  +
</pre>
  +
More information on WP_Query can be found at [[Class_Reference/WP_Query|WP_Query]].
  +
  +
== Related ==
  +
{{Theme Templates}}
   
 
[[Category:Getting Started]]
 
[[Category:Getting Started]]
 
[[Category:WordPress Lessons]]
 
[[Category:WordPress Lessons]]
 
[[Category:Templates]]
 
[[Category:Templates]]
{{Copyedit}}
 

Latest revision as of 09:14, 21 August 2018

A Search Page is a WordPress Page with a custom Page template to give users more information for searching your site.

Things You Need to Know

Different WordPress Themes feature different template files. Some include a search.php template file. This is not a Search Page, it is merely a template that displays the search results. There is also a template file called searchform.php. This is a template file that is often included in the sidebar of many themes and generates the search box form. If there isn't one in your theme, you can easily copy it from the Default theme.

To create your own custom Search Page, you will need to create a Page template to include your search form and the information you want your users to see before they search your site.

Check your WordPress Theme to see if it includes a page.php template file. The Default WordPress Theme does include this template, but many do not. If it does, then follow these instructions. If it does not, we have the information you need to create your own.

Creating a Search Page Template

1. Using a text editor, open the page.php and save as searchpage.php. If you do not have a page.php, you can create one based upon your Theme's index.php template file.
Note: The filename search.php is reserved as a special template name, so avoid its usage; the suggested searchpage.php just makes it easy to recognize in the list of files.
2. After saving it, edit the file:

  • Delete The Loop (i.e. basically everything within your content div), leaving the div tags intact.
  • Add a heading such as "Search Posts" or something similar. You can use an existing class from your CSS style sheet, or create a new one.
  • Copy the following into the content div or any other div that contains the content of your Page:
<?php get_search_form(); ?>
  • At the top of your searchpage.php, before anything else, add this to give your Search Page a heading WordPress will recognize in the Administration Screens:
<?php
/*
Template Name: Search Page
*/
?>

3. Save the file.
4. Upload the file to your theme directory (if you made changes to your style.css style sheet file, upload that, too).

If you create searchpage.php from page.php in Twenty Seventeen, it would be as like as followings:

<?php
/*
Template Name: Search Page
*/
?>
<?php
get_header(); ?>

<div class="wrap">
	<div id="primary" class="content-area">
		<main id="main" class="site-main" role="main">

			<?php get_search_form(); ?>

		</main><!-- #main -->
	</div><!-- #primary -->
</div><!-- .wrap -->

<?php get_footer();

Creating a Search Page

Based on the Search Page Template, we will create the seach page.

  1. In the Administration Screen go to Pages > Add New.
  2. In the title field enter Search.
    Do not write anything in the content area.
  3. While still on the same page, look for Page Attributes on right side menu.
  4. Select the drop-down menu in Template, and select Search Page.
  5. Click the Publish button.

It will show simple search form such as

custom search page 1.jpg

Linking to Your Search Page

You can now make a link to your custom Search Page in several ways.

Using the Page ID

Whether or not you use permalinks, you can link to your new Search Page by using Page ID number of the Page. Insert the next line into your any posts, pages or templates

<a href="index.php?page_id=17" title="Search Page">Search Page</a>

OR you may insert the next line into templates

<a href="<?php echo home_url(); ?>/?page_id=17">Search Page</a>

Using the Page Slug

The Page slug is set in the Edit Page screen. It is the name of the page if you are using Permalinks. You can manually change this. An example of a Page slug link would be:

<a href="/wordpress/search-page/" title="Search Page">Search Page</a>

for any posts, pages or templates when slug is 'search-page'. OR you may insert the next line into templates

<a href="<?php echo home_url(); ?>/wordpress/search-page/" title="Search Page">Search Page</a>

Using wp_list_pages()

If you are using the wp_list_pages() template tag, the page name would be automatically generated in your Pages list.

Customizing Your Search Page

Now that you have created your custom Search Page, you can customize the display. Open your searchpage.php in a text editor and edit it there. Above the get_search_form() function for your searchform.php within the content div, you can add text to help visitors search your site.

<p>
My Site features articles about 
<a title="WordPress Articles" href="/category/wordpress/">WordPress</a>, 
<a title="Web Design Articles" href="/category/web-design/">web page design</a>, 
<a title="Development Articles" href="/category/website-development/">website development</a>,
and <a title="CSS Articles" href="/category/css/">CSS</a>.
</p>
<p>To search my website, please use the form below.</p>

<?php get_search_form(); ?>

custom search page 2.jpg

You might want to include a list of keywords or other information, images, or details to customize your custom Search Page.

Preserving Search Page Results and Pagination

Search results and Pagination may stop working when applying customization to the search template. To avoid these issues the first thing any developer needs to do is add the following code to the start of their Search template to ensure that the original WordPress query is preserved. To customize the query append additional arguments to (array) $search_query. Execute the $search_query through a new $wp_query object, more information on the WP_Query object can be found at WP_Query.

<?php

global $query_string;

wp_parse_str( $query_string, $search_query );
$search = new WP_Query( $search_query );

?>

Additional customization arguments can be found at WP_Query.

Display Total Results

To access the total number of search results from search.php, a search result page, you should retrieve the total number of posts found using the wp_query object.

<?php
global $wp_query;
$total_results = $wp_query->found_posts;
?>

More information on WP_Query can be found at WP_Query.

Related

Template Hierarchy: Category Templates, Tag Templates, Taxonomy Templates, Page Templates, Post Type Templates, Author Templates, Date Templates, Search Templates, 404 Templates, Attachment Templates, Loop Templates