Image Image Image Image Image

Fish Can’t Whistle Limited

Registered Company Number: 7781289

Studio 30
Fazeley Studios
191 Fazeley Street
Birmingham
West Midlands
B5 5SE

 

Scroll to Top

To Top

php

Custom WordPress search with pretty urls

On 06, Dec 2010 | No Comments | In php, WordPress 3.0, wordpress search | By Andy

A recent contact of mine recently posted the following question at wordpress.org -

Hi everyone,

i hope someone here has an idea how to solve this.

I am using a complex plugin combination related to the search engine in wordpress.

Basically, right now i have a URL like
http://www.test.com/search/key1&key2

Is there any way ( maybe a redirection plugin or htaccess rewrite rules ) that i can use the URL http://www.test.com/s/key1-key2

and show the content from http://www.test.com/search/key1&key2 ?

I dont want the URL to change, so the user should see the initial URL, but the plugins ( or WordPress in generall ) should of course use all informations for the search engine.

Looking forward for your ideas or help.

So I gave it some thought and figured it could be done using the 404.php template file.

For the inpatient among you, here is the code (it’s only the first 37 lines that are really of any relevance…) -

<?php
$url = $_SERVER['REQUEST_URI'];
if (substr($url,0, 3) == '/s/'){
 $search=substr($url,3);
 $searchitems = explode('-', $search);
 echo "<title>Search results for ";
 $i=0;
 foreach ($searchitems as $value){
 if($i>0){
 $searchkeys .= " & ";
 }
 $searchkeys .=  $value;
 $i++;
 }
 echo $searchkeys;
 echo "</title>";
 get_header();
 $search_query = new WP_query();
 $search_query->query("s=$searchkeys"); ?>
 <div id="container">
 <div id="content" role="main">
 <?php if ($search_query->have_posts()) : ?>
 <h1><?php echo 'Search results for <span>' . $searchkeys . '</span>'; ?></h1>
 <?php while ($search_query->have_posts()) : $search_query->the_post();?>
 <h2><a href="<?php the_permalink(); ?>" title="<?php the_title();  ?>" rel="bookmark"><?php the_title(); ?></a></h2>
 <div>
 <?php the_excerpt(); ?>
 </div>
 <?php endwhile;
 else : ?>
 <h1>Sorry! No posts were found with the term<?php if($i>1){echo 's';} echo '<span> '.$searchkeys.'</span>'; ?></h1>
 <?php endif;
 wp_reset_query();?>
 </div>
 </div>

<?php }else{

/**
 * The template for displaying 404 pages (Not Found).
 *
 * @package WordPress
 * @subpackage Twenty_Ten
 * @since Twenty Ten 1.0
 */

get_header(); ?>

 <div id="container">
 <div id="content" role="main">

 <div id="post-0">
 <h1><?php _e( 'Not Found', 'twentyten' ); ?></h1>
 <div>
 <p><?php _e( 'Apologies, but the page you requested could not be found. Perhaps searching will help.', 'twentyten' ); ?></p>
 <?php get_search_form(); ?>
 </div><!-- .entry-content -->
 </div><!-- #post-0 -->

 </div><!-- #content -->
 </div><!-- #container -->
 <script type="text/javascript">
 // focus on search field after it has loaded
 document.getElementById('s') && document.getElementById('s').focus();
 </script>

<?php }; ?>

<?php get_footer(); ?>

That is the entirety of the 404.php template file.

Lets look at the important bit -

$url = $_SERVER['REQUEST_URI'];
if (substr($url,0, 3) == '/s/'){
 $search=substr($url,3);
 $searchitems = explode('-', $search);
 echo "<title>Search results for ";
 $i=0;
 foreach ($searchitems as $value){
 if($i>0){
 $searchkeys .= " & ";
 }
 $searchkeys .=  $value;
 $i++;
 }
 echo $searchkeys;
 echo "</title>";
 get_header();
 $search_query = new WP_query();
 $search_query->query("s=$searchkeys"); ?>
 <div id="container">
 <div id="content" role="main">
 <?php if ($search_query->have_posts()) : ?>
 <h1><?php echo 'Search results for <span>' . $searchkeys . '</span>'; ?></h1>
 <?php while ($search_query->have_posts()) : $search_query->the_post();?>
 <h2><a href="<?php the_permalink(); ?>" title="<?php the_title();  ?>" rel="bookmark"><?php the_title(); ?></a></h2>
 <div>
 <?php the_excerpt(); ?>
 </div>
 <?php endwhile;
 else : ?>
 <h1>Sorry! No posts were found with the term<?php if($i>1){echo 's';} echo '<span> '.$searchkeys.'</span>'; ?></h1>
 <?php endif;
 wp_reset_query();?>
 </div>
 </div>

<?php }else{

Quite simply what that does is look to see if the string ‘/s/’ is in the url (as Hamed specified in his question), puts the various keys in to an array, loops the array building the correct argument for the custom query that we then implement using wp_query.

Of course how you get to the url to initialise this code isn’t in here. That will differ for each person depending on how they are using it. However a simple redirect upon submission of the search form adding the keys to the url would probably be what i would do…

Submit a Comment