Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Need help creating a custom search WordPress

  • SOLVED

On this page: http://www.vipmagazine.com/search-spectacular-locations/ the client would like to have visitors search posts by Business Name, which is always the title of the post. I need a search box under the title "Search by Business Name" and then have a page of results generated.

Currently if you search, for example, "Celebrations" it returns all posts or pages with Celebrations in the text. She would like it to ONLY show the posts with Celebrations [or whatever name] in the TITLE.

Is this doable?

I've already tried umpteen search plugins that don't work, so that option won't fly :)

Answers (5)

2010-10-28

Lew Ayotte answers:

Yeah, definitely doable... I'll throw some code up for you in a moment.


Lew Ayotte comments:

In functions.php:

//search by business name
//You may need to do a little more work here, if you want to include only certain categories, etc.
function get_business_name_posts( $search_term = '%' ) {
global $wpdb;

$search_term = preg_replace('/\s+/i', '%', trim(mysql_real_escape_string($search_term)));

$query = "SELECT DISTINCT p.ID " .
"FROM $wpdb->posts p " .
"WHERE post_status = 'publish' " .
"AND post_title LIKE '%" . $search_term . "%' " .
"ORDER BY post_title;";

$business_ids = $wpdb->get_results($query, OBJECT);

return $business_ids;
}


Then, I created a template called "Business Search" and set a Page to use it, but you can incorporate this code into your existing files...

<?php
/*
Template Name: Business Search
*/

get_header();

if ( isset($_GET['b']) ) {
$business_name = htmlspecialchars($_GET['b']);
?>

Search by Business Name:
<form action="<?php echo htmlspecialchars( $_SERVER['REDIRECTX_URL'] ); ?>" method="get">
<input type="text" name="b" id="b" value="<?php echo $business_name; ?>">
<input type="submit" name="submit" id="submit">
</form>

<?php
$businesses = get_business_name_posts( $business_name );

foreach ( $businesses as $business ) {
$post = get_post( $business->ID );

echo "<h3>" . $business->ID . $post->post_title . "</h3";
}
} else {
?>

Search by Business Name:
<form action="<?php echo htmlspecialchars( $_SERVER['REDIRECTX_URL'] ); ?>" method="get">
<input type="text" name="b" id="b" value="">
<input type="submit" name="submit" id="submit">
</form>

<?php
}

get_footer(); ?>


The working example is here: http://latest.phrugal.com/business-search/
I added a few "businesses" named Microsoft, MircoStuff, Fisher Price, AT&T

So if you search for "t" you'll get Microsoft, MicroStuff, and AT&T... if you search for "Microsoft" you'll get just Microsoft, etc.

I hope this helps...


Jill Chongva comments:

To ask it to only search category 17, where would I add that part? I only know enough php to be dangerous


Jill Chongva comments:

Here is the code for my page template:

<?php get_header(); ?>

<div id="content">

<div id="contentleft">

<div class="postarea">

<?php include(TEMPLATEPATH."/breadcrumb.php");?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1><br />

<?php the_content(__('Read more', 'studiopress'));?><div class="clear"></div><?php edit_post_link(__('(Edit)', 'studiopress'), '', ''); ?>
<?php endwhile; else: ?>

<p><?php _e('Sorry, no posts matched your criteria.', 'studiopress'); ?></p><?php endif; ?>

</div>

</div>

<?php include(TEMPLATEPATH."/sidebar.php");?>

</div>

<?php // The main column ends ?>

<?php get_footer(); ?>


To convert this to the search page, what needs to stay and what needs to go?

I took the code you gave me (thank you!) and made a business search page, but this is what happened [[LINK href="http://www.vipmagazine.com/search-spectacular-locations/?b=Celebrations&submit=Submit+Query"]][[/LINK]]

so I know I've mucked it up :)

j :)


Lew Ayotte comments:

Try changing the function to this (for category 17)

function get_business_name_posts( $search_term = '%' ) {
global $wpdb;

$search_term = preg_replace('/\s+/i', '%', trim(mysql_real_escape_string($search_term)));

$query = "SELECT DISTINCT p.ID " .
"FROM $wpdb->posts p " .
"LEFT JOIN $wpdb->term_taxonomy t on t.object_id = p.ID " .
"WHERE post_status = 'publish' " .
"AND post_title LIKE '%" . $search_term . "%' " .
"AND t.term_taxonomy_id = '17' " .
"ORDER BY post_title;";

$business_ids = $wpdb->get_results($query, OBJECT);

return $business_ids;
}


Your template (try this):

<?php
/*
Template Name: Business Search
*/

get_header(); ?>

<div id="content">

<div id="contentleft">

<div class="postarea">

<?php include(TEMPLATEPATH."/breadcrumb.php");

if ( isset($_GET['b']) ) {
$business_name = htmlspecialchars($_GET['b']);
?>

Search by Business Name:
<form action="<?php echo htmlspecialchars( $_SERVER['REDIRECTX_URL'] ); ?>" method="get">
<input type="text" name="b" id="b" value="<?php echo $business_name; ?>">
<input type="submit" name="submit" id="submit">
</form>

<?php
$businesses = get_business_name_posts( $business_name );

foreach ( $businesses as $business ) {
$post = get_post( $business->ID );

echo "<h1>" . $post->post_title . "</h1>";
}
} else {
?>

Search by Business Name:
<form action="<?php echo htmlspecialchars( $_SERVER['REDIRECTX_URL'] ); ?>" method="get">
<input type="text" name="b" id="b" value="">
<input type="submit" name="submit" id="submit">
</form>

<?php
}
?>

</div>

</div>

<?php include(TEMPLATEPATH."/sidebar.php");?>

</div>

<?php // The main column ends ?>

<?php get_footer(); ?>


Lew Ayotte comments:

Correction, the function should be:

function get_business_name_posts( $search_term = '%' ) {
global $wpdb;

$search_term = preg_replace('/\s+/i', '%', trim(mysql_real_escape_string($search_term)));

$query = "SELECT DISTINCT p.ID " .
"FROM $wpdb->posts p " .
"LEFT JOIN $wpdb->term_relationships t on t.object_id = p.ID " .
"WHERE p.post_status = 'publish' " .
"AND p.post_title LIKE '%" . $search_term . "%' " .
"AND t.term_taxonomy_id = '17' " .
"ORDER BY post_title;";

$business_ids = $wpdb->get_results($query, OBJECT);

return $business_ids;
}


Jill Chongva comments:

So close - it's searching only post titles, but it's only showing the post title for the returned info. Can it show the entire post instead?

[[LINK href="http://www.vipmagazine.com/search-spectacular-locations/"]][[/LINK]]


Lew Ayotte comments:

Try changing the foreach in the template file to this:


foreach ( $businesses as $business ) {
$post = get_post( $business->ID );
?>

<h1><?php echo $post->post_title; ?></h1><br />

<?php echo $post->post_content; ?>
<div class="clear"></div>
<?php edit_post_link(__('(Edit)', 'studiopress'), '', ''); ?>

<?php
}


Jill Chongva comments:

Can you post the whole template page code? I'm breaking it again :(


Lew Ayotte comments:

here you go :)

<?php
/*
Template Name: Business Search
*/

get_header();

if ( isset($_GET['b']) ) {
$business_name = htmlspecialchars($_GET['b']);
?>

Search by Business Name:
<form action="<?php echo htmlspecialchars( $_SERVER['REDIRECTX_URL'] ); ?>" method="get">
<input type="text" name="b" id="b" value="<?php echo $business_name; ?>">
<input type="submit" name="submit" id="submit">
</form>

<?php
$businesses = get_business_name_posts( $business_name );

foreach ( $businesses as $business ) {
$post = get_post( $business->ID );
?>

<h1><?php echo $post->post_title; ?></h1><br />

<?php echo $post->post_content; ?>
<div class="clear"></div>
<?php edit_post_link(__('(Edit)', 'studiopress'), '', ''); ?>

<?php
}
} else {
?>

Search by Business Name:
<form action="<?php echo htmlspecialchars( $_SERVER['REDIRECTX_URL'] ); ?>" method="get">
<input type="text" name="b" id="b" value="">
<input type="submit" name="submit" id="submit">
</form>

<?php
}

get_footer(); ?>


Lew Ayotte comments:

oh wait, i pasted the wrong one... :)


Lew Ayotte comments:

<?php get_header(); ?>

<div id="content">

<div id="contentleft">

<div class="postarea">

<?php include(TEMPLATEPATH."/breadcrumb.php");

if ( isset($_GET['b']) ) {
$business_name = htmlspecialchars($_GET['b']);
?>

Search by Business Name:
<form action="<?php echo htmlspecialchars( $_SERVER['REDIRECTX_URL'] ); ?>" method="get">
<input type="text" name="b" id="b" value="<?php echo $business_name; ?>">
<input type="submit" name="submit" id="submit">
</form>

<?php
$businesses = get_business_name_posts( $business_name );

foreach ( $businesses as $business ) {
$post = get_post( $business->ID );
?>

<h1><?php echo $post->post_title; ?></h1><br />

<?php echo $post->post_content; ?>
<div class="clear"></div>
<?php edit_post_link(__('(Edit)', 'studiopress'), '', ''); ?>

<?php
}
} else {
?>

Search by Business Name:
<form action="<?php echo htmlspecialchars( $_SERVER['REDIRECTX_URL'] ); ?>" method="get">
<input type="text" name="b" id="b" value="">
<input type="submit" name="submit" id="submit">
</form>

<?php
}
?>

</div>

</div>

<?php include(TEMPLATEPATH."/sidebar.php");?>

</div>

<?php // The main column ends ?>

<?php get_footer(); ?>


Jill Chongva comments:

Awesome - thank you! The results page is now not showing the Google map and the info below the map.

Where do I look to change that?


Lew Ayotte comments:

Try this:

<?php get_header(); ?>

<div id="content">

<div id="contentleft">

<div class="postarea">

<?php include(TEMPLATEPATH."/breadcrumb.php");

if ( isset($_GET['b']) ) {
$business_name = htmlspecialchars($_GET['b']);
?>

Search by Business Name:
<form action="<?php echo htmlspecialchars( $_SERVER['REDIRECTX_URL'] ); ?>" method="get">
<input type="text" name="b" id="b" value="<?php echo $business_name; ?>">
<input type="submit" name="submit" id="submit">
</form>

<?php
$businesses = get_business_name_posts( $business_name );

foreach ( $businesses as $business ) {
$post = get_post( $business->ID );
?>

<h1><?php echo $post->post_title; ?></h1><br />

<?php echo apply_filters('the_content', $post->post_content); ?>
<div class="clear"></div>
<?php edit_post_link(__('(Edit)', 'studiopress'), '', ''); ?>

<?php
}
} else {
?>

Search by Business Name:
<form action="<?php echo htmlspecialchars( $_SERVER['REDIRECTX_URL'] ); ?>" method="get">
<input type="text" name="b" id="b" value="">
<input type="submit" name="submit" id="submit">
</form>

<?php
}
?>

</div>

</div>

<?php include(TEMPLATEPATH."/sidebar.php");?>

</div>

<?php // The main column ends ?>

<?php get_footer(); ?>

2010-10-28

Cosmin Popovici answers:

Install [[LINK href="http://wordpress.org/extend/plugins/wp-custom-fields-search/"]]WP Custom Fields Search[[/LINK]].


Now, when creating a new search form to place under your heading, just be sure to select "Title" from the Data Field. (you need to click show/hide config to show advanced options)

This sets the plugin to only search the Title of your posts (you can see the default is set to All Fields, which searches your entire post)


Jill Chongva comments:

Like I said, installing a plugin won't work - I've tried that one and it never show search results - only boots people back to the home page.

2010-10-28

Jermaine Oppong answers:

You can Install [[LINK href="http://wordpress.org/extend/plugins/search-everything/"]]Search Everything[[/LINK]]

This allows you to search through pages, tags, custom taxonomies, categories, comments, drafts, excerpts, CUSTOM FIELDS and more!

Definetely Check that plugin out. Should solve everything. Thats what I'm using on my blog and works brilliantly. Also has a settings panel which is very easy to use.

Or simply use this code in your functions.php file:



add_action('pre_get_posts','sort_searchresult_by_title');

function sort_searchresult_by_title($k) {

if(is_search()) {

$k->query_vars['orderby'] = 'title';

$k->query_vars['order'] = 'DESC';

}

}



Paste that into your functions.php file. You can change the order from 'DESC' to 'ASC' if you want it listing the other direction. All the best.


Jill Chongva comments:

Tried that and it wouldn't work - that's why I posted this and asked people not to recommend plugins.

2010-10-29

idt answers:

Try this:

1. Open wp-includes/query.php

2. Search for:

$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";


3. Replace with:

$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";


idt comments:

Oops I can't seem to edit my post.

If understand your question correctly, you want it to search from the post title only, right? If so, try my code above.

Please don't forget to create a backup of the query.php file.


idt comments:

or better yet create an action to posts_search


add_action( 'posts_search', 'dont_search_content', 10000, 1 );
function dont_search_content( $search_sql ) {
global $wpdb;

if( strpos( $search_sql, 'post_content LIKE' ) )
$search_sql = preg_replace( "|OR \({$wpdb->posts}.post_content LIKE '(.+)'\)|", '', $search_sql );

return $search_sql;
}


idt comments:

For the complete steps, please do these:

1. Copy this block of code into a php file(you-decide-the-filename.php)

2. Paste this block codes into the file

<?php
/*
Plugin Name: Search From Title Only
*/

add_action( 'posts_search', 'dont_search_content', 10000, 1 );

function dont_search_content( $search_sql ) {

global $wpdb;



if( strpos( $search_sql, 'post_content LIKE' ) )

$search_sql = preg_replace( "|OR \({$wpdb->posts}.post_content LIKE '(.+)'\)|", '', $search_sql );



return $search_sql;

}

?>



3. Upload the file to your wp-content/plugins folder.

4. Activcate the plugin

2010-10-29

Gabriel Reguly answers:

Hi Jill Chongva,

Here is the full template.php from Lew Ayotte.

I will test it soon.

Regards,
Gabriel Reguly


<?php
/*
Template Name: Business Search
*/


get_header(); ?>

<div id="content">
<div id="contentleft">
<div class="postarea">
<?php
include(TEMPLATEPATH."/breadcrumb.php");
if ( isset($_GET['b']) ) {
$business_name = htmlspecialchars($_GET['b']);
?>
Search by Business Name:
<form action="<?php echo htmlspecialchars( $_SERVER['REDIRECTX_URL'] ); ?>" method="get">
<input type="text" name="b" id="b" value="<?php echo $business_name; ?>">
<input type="submit" name="submit" id="submit">
</form>
<?php
$businesses = get_business_name_posts( $business_name );
foreach ( $businesses as $business ) {
$post = get_post( $business->ID );
?>
<h1><?php echo $post->post_title; ?></h1><br />
<?php echo $post->post_content; ?>
<div class="clear"></div>
<?php edit_post_link('Edit', '', ''); ?>
<?php
}
} else {
?>
Search by Business Name:
<form action="<?php echo htmlspecialchars( $_SERVER['REDIRECTX_URL'] ); ?>" method="get">
<input type="text" name="b" id="b" value="">
<input type="submit" name="submit" id="submit">
</form>
<?php
}
?>
</div>
</div>
<?php include(TEMPLATEPATH."/sidebar.php");?>
</div>
<?php // The main column ends ?>
<?php get_footer(); ?>


Gabriel Reguly comments:

After my quick test it seems that the code is not broken, can you confirm it?

Remember this is code from Lew Ayotte, I have just made it one piece, as you asked.


Gabriel Reguly comments:

After my quick test it seems that the code is not broken, can you confirm it?

Remember this is code from Lew Ayotte, I have just made it one piece, as you asked.


Gabriel Reguly comments:

Lew Ayotte just posted the code, please disregard my help.

Regarding the $5 prize, I was meaning the task of formatting, assembling in one piece and testing if it was correct.

The original $60 seems to me a fair price for all Lew Ayotte work, but only him can confirm that.