我正在为一个项目使用模板(不幸的是客户的想法),这个模板有一个下拉搜索部分供用户搜索自定义帖子(属性)。唯一的问题是,没有关键字框供用户按关键字搜索。
我正在尝试为用户集成一个搜索功能,以通过关键字进行搜索,但是事实证明这比我最初想象的要困难得多。我不是最精通wordpress(我使用EE)。
有人可以帮我将关键字搜索功能集成到以下代码中:
customsearchform.php
<form id="search" action="<?php bloginfo('url'); ?>/<?php echo $slugname; ?>" method="post">
<label>Keywords</label>
<input type="text" name="keywords" style="width:190px;margin-bottom:10px;" />
<div id="location">
<label><?php echo get_option('wp_location_text') ?></label>
<?php
$location_level1 = get_option('wp_location_level1');
$arr_location_level1 = explode("\n", $location_level1);
echo "<select id='location_level1' name='location_level1'>";
$counter = 0;
foreach ($arr_location_level1 as $item => $value) {
if($counter == 0) {
echo "<option value='' selected='selected'>Anywhere</option>";
echo "<option value='".$value."'>".$value."</option>";
} else {
echo "<option value='".$value."'>".$value."</option>";
}
$counter = $counter + 1;
}
echo "</select>";
?>
<?php if(get_option("wp_secondary_location") == 'Enable') { ?>
<div id="location_level2_drop_down">
<select id="location_level2" name="location_level2">
</select>
</div>
<?php } ?>
</div><!-- end location -->
<?php if (get_option('wp_rentbuy') == "Show") { ?>
<div id="rentorbuy">
<label><?php echo get_option('wp_rentorbuy_text') ?></label>
<select name="rentbuy">
<option value="">Any</option>
<option value="rent">Rent</option>
<option value="buy">Buy</option>
</select>
</div><!-- end rentorbuy -->
<?php } ?>
<?php
$prices = get_option('wp_price');
$arr_prices = explode("\n", $prices);
foreach ($arr_prices as $price) {
$priceoptions = $priceoptions . "<option value='" . $price . "'>" . $currencysymbol . number_format($price) . "</option>";
}
$prices2 = get_option('wp_price2');
$arr_prices2 = explode("\n", $prices2);
foreach ($arr_prices2 as $price2) {
$priceoptions2 = $priceoptions2 . "<option value='" . $price2 . "'>" . $currencysymbol . number_format($price2) . "</option>";
}
?>
<div id="bedsbathsrow">
<div id="beds">
<label><?php echo get_option('wp_bedrooms_text') ?></label>
<select name="beds">
<option value="">Any</option>
<option value="1">1+</option>
<option value="2">2+</option>
<option value="3">3+</option>
<option value="4">4+</option>
<option value="5">5+</option>
</select>
</div><!-- end beds -->
<div id="propertytypesection">
<label><?php echo get_option('wp_propertytype_text') ?></label>
<?php
$propertytype = get_option('wp_propertytype');
$arr_propertytype = explode("\n", $propertytype);
$counter = 0;
echo "<select id='propertytype' name='propertytype'>";
foreach ($arr_propertytype as $item => $value) {
if($counter == 0) {
echo "<option value='' selected='selected'>Any</option>";
echo "<option value='".$value."'>".$value."</option>";
} else {
echo "<option value='".$value."'>".$value."</option>";
}
$counter = $counter + 1;
}
echo "</select>";
?>
</div><!-- end propertytype -->
</div><!-- end bedsbathsrow -->
<!-- BUY PRICES -->
<div id="buyprices">
<!-- minimum price dropdown menu -->
<div class="pricemin">
<label><?php echo get_option('wp_minimumprice_text') ?></label>
<select name="minprice_buy">
<option value="0">No Min</option>
<?php echo $priceoptions ?>
</select>
</div><!-- end pricemin -->
<!-- Maximum price dropdown menu -->
<div class="pricemax">
<label><?php echo get_option('wp_maximumprice_text') ?></label>
<select name="maxprice_buy">
<option value="99999999999999">No Max</option>
<?php echo $priceoptions ?>
</select>
</div><!-- end pricemax -->
</div> <!-- end buyprices -->
<!-- RENT PRICES -->
<?php if (trim(get_option('wp_rentbuy') == "Show")) { ?>
<!-- minimum price dropdown menu -->
<div id="rentprices" style="display: none;">
<div class="pricemin">
<label><?php echo get_option('wp_minimumprice_text') ?></label>
<select name="minprice_rent">
<!-- do not edit the next line -->
<option value="0">No Min</option>
<?php echo $priceoptions2 ?>
</select>
</div><!-- end pricemin -->
<!-- Maximum price dropdown menu -->
<div class="pricemax">
<label><?php echo get_option('wp_maximumprice_text') ?></label>
<select name="maxprice_rent">
<!-- do not edit the next line -->
<option value="9999999999999">No Max</option>
<?php echo $priceoptions2 ?>
</select>
</div><!-- end pricemax -->
</div><!-- end RENT PRICES -->
<?php } ?>
<div id="propertytyperow">
<div><input value="<?php echo get_option('wp_searchbutton_text') ?>" type="submit" /></div>
</div><!-- end propertytyperow -->
</form>
search_query.php
<?php
if (isset($_COOKIE['location_level1']) && $_COOKIE['location_level1'] != '') {
$search_location_level1 = $_COOKIE['location_level1'];
} else {
$search_location_level1 = trim($_POST['location_level1']);
}
if (isset($_COOKIE['location_level2']) && $_COOKIE['location_level2'] != '') {
$search_location_level2 = $_COOKIE['location_level2'];
} else {
$search_location_level2 = trim($_POST['location_level2']);
}
if (isset($_COOKIE['beds']) && $_COOKIE['beds'] != '') {
$search_bedrooms = $_COOKIE['beds'];
} else {
$search_bedrooms = trim($_POST['beds']);
}
if (isset($_COOKIE['baths']) && $_COOKIE['baths'] != '') {
$search_bathrooms = $_COOKIE['baths'];
} else {
$search_bathrooms = trim($_POST['baths']);
}
if (isset($_COOKIE['minprice_buy']) && $_COOKIE['minprice_buy'] != '') {
$search_pricemin_buy = $_COOKIE['minprice_buy'];
} else {
$search_pricemin_buy = '0';
}
if (isset($_COOKIE['maxprice_buy']) && $_COOKIE['maxprice_buy'] != '') {
$search_pricemax_buy = $_COOKIE['maxprice_buy'];
} else {
$search_pricemax_buy = '99999999999999';
}
if (isset($_COOKIE['minprice_rent']) && $_COOKIE['minprice_rent'] != '') {
//echo $_COOKIE['minprice_rent'] . "...";
$search_pricemin_rent = $_COOKIE['minprice_rent'];
} else {
//echo "222";
$search_pricemin_rent = '0';
}
if (isset($_COOKIE['maxprice_rent']) && $_COOKIE['maxprice_rent'] != '') {
$search_pricemax_rent = $_COOKIE['maxprice_rent'];
} else {
$search_pricemax_rent = '99999999999999';
}
if (isset($_COOKIE['propertytype']) && $_COOKIE['propertytype'] != '') {
$search_propertytype = $_COOKIE['propertytype'];
} else {
$search_propertytype = trim($_POST['propertytype']);
}
if (get_option('wp_rentbuy') == "Show") {
if (isset($_COOKIE['rentbuy']) && $_COOKIE['rentbuy'] != '') {
$search_buyorrent = $_COOKIE['rentbuy'];
} else {
$search_buyorrent = trim($_POST['rentbuy']);
}
} else {
$search_buyorrent = '';
}
$checkalllistings = $_GET['alllistings'];
if ($checkalllistings == true) {
$search_location_level1 = '';
$search_location_level2 = '';
$search_bedrooms = '';
$search_bathrooms = '';
$search_pricemin_buy = '0';
$search_pricemax_buy = '99999999999999';
$search_pricemin_rent = '0';
$search_pricemax_rent = '99999999999999';
$search_propertytype = '';
$search_buyorrent = '';
}
?>
<?php
$_ids = array();
function getIds( $query ) {
global $wpdb;
$searchresults = $wpdb->get_results($query, ARRAY_A);
if ( !empty ($searchresults) ) {
foreach( $searchresults as $_post ) {
$tmp[] = $_post['ID'];
}
}
return $tmp;
}
global $wpdb;
//$query ="SELECT p.* FROM $wpdb->posts p, $wpdb->postmeta p1 WHERE p.ID = p1.post_id AND p.post_type = 'listing' AND p.post_status = 'publish'";
$query ="SELECT p.* FROM $wpdb->posts p WHERE p.post_type = 'listing' AND p.post_status = 'publish'";
$all = getIds( $query );
$_ids = ( !empty($all) ? ( !empty($_ids) ? array_intersect( $_ids, $all) : $all ) : "" );
if ($search_buyorrent != "") {
$query = "SELECT p.* FROM $wpdb->posts p, $wpdb->postmeta p1
WHERE p.ID = p1.post_id AND p1.meta_key = 'rob_value' AND p1.meta_value = '$search_buyorrent'";
$spm = getIds( $query );
$_ids = ( !empty($spm) ? ( !empty($_ids) ? array_intersect( $_ids, $spm) : "" ) : "" );
}
if ($search_buyorrent == "buy" || $search_buyorrent == "") {
$query ="SELECT p.* FROM $wpdb->posts p, $wpdb->postmeta p1
WHERE p.ID = p1.post_id AND p1.meta_key='price_value' AND convert(p1.meta_value, signed) BETWEEN '$search_pricemin_buy' AND '$search_pricemax_buy'";
$spm = getIds( $query );
$_ids = ( !empty($spm) ? ( !empty($_ids) ? array_intersect( $_ids, $spm) : "" ) : "" );
}
if ($search_buyorrent == "rent") {
$query ="SELECT p.* FROM $wpdb->posts p, $wpdb->postmeta p1
WHERE p.ID = p1.post_id AND p1.meta_key='price_value' AND convert(p1.meta_value, signed) BETWEEN '$search_pricemin_rent' AND '$search_pricemax_rent'";
$spm = getIds( $query );
$_ids = ( !empty($spm) ? ( !empty($_ids) ? array_intersect( $_ids, $spm) : "" ) : "" );
}
if($search_location_level1 != '')
{
$search_location_level1 = trim($search_location_level1);
$query ="SELECT p.* FROM $wpdb->posts p, $wpdb->postmeta p1
WHERE p.ID = p1.post_id AND p1.meta_key = 'location_level1_value' AND p1.meta_value = '$search_location_level1'";
$sll1 = getIds( $query );
$_ids = ( !empty($sll1) ? ( !empty($_ids) ? array_intersect( $_ids, $sll1) : "" ) : "" );
}
if($search_location_level2 != '')
{
$search_location_level2 = trim($search_location_level2);
$query ="SELECT p.* FROM $wpdb->posts p, $wpdb->postmeta p1
WHERE p.ID = p1.post_id AND p1.meta_key='location_level2_value' AND trim(p1.meta_value)='$search_location_level2'";
$sll2 = getIds( $query );
$_ids = ( !empty($sll2) ? ( !empty($_ids) ? array_intersect( $_ids, $sll2) : "" ) : "" );
}
if($search_bedrooms != '')
{
$query ="SELECT p.* FROM $wpdb->posts p, $wpdb->postmeta p1
WHERE p.ID = p1.post_id AND p1.meta_key='beds_value' AND p1.meta_value >= '$search_bedrooms'";
$sbr = getIds( $query );
$_ids = ( !empty($sbr) ? ( !empty($_ids) ? array_intersect( $_ids, $sbr) : "" ) : "" );
}
if($search_bathrooms != '')
{
$query ="SELECT p.* FROM $wpdb->posts p, $wpdb->postmeta p1
WHERE p.ID = p1.post_id AND p1.meta_key='baths_value' AND p1.meta_value >='$search_bathrooms'";
$sbt = getIds( $query );
$_ids = ( !empty($sbt) ? ( !empty($_ids) ? array_intersect( $_ids, $sbt) : "" ) : "" );
}
if($search_propertytype != '')
{
$search_propertytype = trim($search_propertytype);
$query ="SELECT p.* FROM $wpdb->posts p, $wpdb->postmeta p1
WHERE p.ID = p1.post_id AND p1.meta_key='propertytype_value' AND p1.meta_value='$search_propertytype'";
$sptt = getIds( $query );
$_ids = ( !empty($sptt) ? ( !empty($_ids) ? array_intersect( $_ids, $sptt) : "" ) : "" );
}
?>
<a name="headeranchor"></a>
<?php if ($checkalllistings == true) {
$alllistings = true;
if (count($_ids) > 3) {
$results = " (" . count($_ids) . ")";
} else {
$results = "";
}
?>
<h2 id="heading_searchresults" class="results"><?php echo get_option('wp_alllistings') . $results; ?></h2>
<?php } else { ?>
<h2 id="heading_searchresults" class="results"><?php echo get_option('wp_searchresults') . $results; ?></h2>
<?php } ?>
<?php
if($resultsorder) {
//get value from order dropdown on search results page
$resultsorder = $resultsorder;
} else {
$resultsorder = get_option('wp_searchorder');
}
switch ($resultsorder) {
case "Price Descending":
$metakey = 'price_value';
$order = 'DESC';
$orderby = 'meta_value_num';
break;
case "Price Ascending":
$metakey = 'price_value';
$order = 'ASC';
$orderby = 'meta_value_num';
break;
case "Date Descending":
$metakey = '';
$order = 'DESC';
$orderby = 'date';
break;
case "Date Ascending":
$metakey = '';
$order = 'ASC';
$orderby = 'date';
break;
case "Random":
$metakey = '';
$order = '';
$orderby = 'rand';
break;
}
if (!empty($_ids) && !$alllistings) {
$wpq = array ('post_type' => 'listing', 'meta_key' => $metakey, 'orderby' => $orderby, 'order' => $order, 'post__in' => $_ids, 'post_status' => 'publish', 'paged' => $paged, 'posts_per_page' => get_option('wp_searchresultsperpage') );
} elseif (empty($_ids) && !$alllistings) {
// $_ids array is empty because search got no results
// $_ids array will be empty if page is an "All Listings" page. Don't run this code if is All Listings because All Listings will show all listings. This code will display "no results found"
$wpq = array ('post_type' =>'listing', 'meta_key' => $metakey, 'orderby' => $orderby, 'order' => $order, 'post__in' => array('0'),'post_status' => 'publish', 'paged' => $paged, 'posts_per_page' => get_option('wp_searchresultsperpage'));
} elseif ($alllistings) {
// This is an All Listings page, so show all results
$wpq = array ('post_type' =>'listing', 'paged' => $paged, 'meta_key' => $metakey, 'orderby' => $orderby, 'order' => $order, 'post_status' => 'publish', 'posts_per_page' => get_option('wp_searchresultsperpage'));
}
query_posts($wpq);
//$listing = new WP_Query($wpq);
?>
<?php
if ($search_location_level1 != "") { ?>
<script type="text/javascript">
$("#location_level2").load("<?php bloginfo('template_url');?>/secondary_search_locations/<?php echo $search_location_level1 ?>.txt");
</script>
<?php } ?>
<?php
if ($search_buyorrent == "buy" || $search_buyorrent == "") { ?>
<script type="text/javascript">
$('#buyprices').show();
$('#rentprices').hide();
</script>
<?php } else { ?>
<script type="text/javascript">
$('#buyprices').hide();
$('#rentprices').show();
</script>
<?php } ?>
<script type="text/javascript">
function remember( selector ){
$(selector).each(
function(){
//if this item has been cookied, restore it
var name = $(this).attr('name');
if( $.cookie( name ) ){
$(this).val( $.cookie(name) );
}
//assign a change function to the item to cookie it
$(this).change(
function(){
$.cookie(name, $(this).val(), { path: '/', expires: 365 });
}
);
}
);
}
<?php
if ($search_buyorrent == "buy" || $search_buyorrent == "") { ?>
remember( '[name=minprice_buy], [name=maxprice_buy]' );
<?php } ?>
<?php if ($search_buyorrent == "rent") { ?>
remember( '[name=minprice_rent], [name=maxprice_rent]' );
<?php } ?>
</script>
<?php
/*
echo '<pre>';
print $query;
echo '</pre>';
*/
?>
提前致谢!