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

Search Custom Fields WordPress

  • SOLVED

I have a category of posts, each post containing information about a company offering a particular activity or service (go-karting, paintballing, etc.)

Two of the custom fields attached to east of these posts are time (day/night) and location (various cities in the UK).

I'd like to make a search form where a user could type in search keywords, then select day/night from a dropdown, and also a particular location from another dropdown, so that their search results are filtered to only include day/night listings for that particular location.

Answers (1)

2010-06-18

Oleg Butuzov answers:

on a search tempalte just make a custom query_posts with meta_key meta_value properties getted from your form (day and night)

reference article
http://www.smashingmagazine.com/2009/06/10/10-useful-wordpress-loop-hacks/

<?php query_posts('s='.$_GET['s'].'&showposts=-1&meta_key=metakeyofyourselect&meta_value='.addslashes($_GET['meta_value'])); ?>
<?php if (have_posts()) { ?>
<?php while (have_posts()) {
<?php the_post(); ?>
<?php } ?>
<?php } ?>


and of course also need to add category_id to your custom query - if you want to search for a specific category posts.


Dan Davies comments:

Alas that only allows me to query one meta key/value.


Oleg Butuzov comments:

there one trick...

you also can change actual query...

use add_filter('query', 'queryfilterqueryfunction');
query_posts(.....);
use remove_filter('query', 'queryfilterqueryfunction');

in queryfilterqueryfunction witch is get sql as argment you can change actual sql. i did usch think in few projects where i needed to find a posts by multiple custom taxonomies. this work require regular explressions or good knowleges of working with strings...


Dan Davies comments:

Okay - I think I can work with that. Is there any way I can create a second search.php so that this custom search query doesn't affect the site-wide standard search?


Oleg Butuzov comments:

1) create a special page, for example search-england
2) set action for the form for this page (action="search-england");
3) before running loop code, run custom query...


---
here is a few examples of custom query translformation fucntion.

1) this is change 'category' to 'alboum', (alboum is custom taxonomy), a source of query is query_posts('showposts=-1&post_type=diskografyia&cat='.$alboum->term_id);
change function
function disco_select($sql){
return str_replace("'category'", "'alboum'", $sql);
}



2) multiple taxonomies select (advanced query filter function)

function fcg_query_filter($sql){
global $wpdb;
/*
if (strpos($sql, "AND ".$wpdb->prefix."term_taxonomy.taxonomy = 'post_tag'") !== false){
$sql = str_replace(" WHERE ", " INNER JOIN wp_terms ON (wp_terms.term_id = wp_term_taxonomy.term_id ) WHERE ", $sql);
//var_dump(get_terms($_POST['orderby'], 'fields=all&order='.$_POST['order']));

$sql = str_replace("AND ".$wpdb->prefix."term_taxonomy.taxonomy = 'post_tag'", "", $sql);
$sql = str_replace("ORDER BY ".$wpdb->prefix."posts.post_date DESC", "[orderby]", $sql);
$sql = str_replace("SQL_CALC_FOUND_ROWS ", "SQL_CALC_FOUND_ROWS wp_terms.name, ", $sql);

//$order = ' ORDER BY field('.$wpdb->term_taxonomy.'.term_id, '.implode(',', get_terms($_POST['orderby'], 'fields=ids&order='.$_POST['order'])).")";
//$sql = str_replace("LIMIT ", "ORDER BY wp_terms.name ".$_POST['order']." LIMIT ", $sql);
$sql = preg_replace('/AND '.$wpdb->term_taxonomy.'.term_id IN (.*?) AND/si', 'AND [terms] AND', $sql);
$sql = str_replace('[terms]', $wpdb->term_taxonomy.'.term_id IN ('.implode(',', get_terms($_POST['orderby'], 'fields=ids&order='.$_POST['order']) ).')', $sql);
$order = "ORDER BY wp_terms.name ".$_POST['order'];
$sql = str_replace("[orderby]", $order, $sql);
echo '<br><br><br>';
echo $sql;
echo '<br><br><br>';
}
*/


if (strpos($sql, "AND ".$wpdb->prefix."term_taxonomy.taxonomy = 'post_tag'") !== false){

$sql = str_replace(" WHERE ", " INNER JOIN wp_terms ON (wp_terms.term_id = wp_term_taxonomy.term_id ) WHERE ", $sql);
$sql = str_replace("AND ".$wpdb->prefix."term_taxonomy.taxonomy = 'post_tag'", "", $sql);
$sql = str_replace("ORDER BY ".$wpdb->prefix."posts.post_date DESC", "[orderby]", $sql);
$sql = str_replace("SQL_CALC_FOUND_ROWS ", "SQL_CALC_FOUND_ROWS wp_terms.name, ", $sql);
$sql = preg_replace('/AND '.$wpdb->term_taxonomy.'.term_id IN (.*?) AND/si', 'AND [terms] AND', $sql);
$sql = str_replace('[terms]', $wpdb->term_taxonomy.'.term_id IN ('.implode(',', get_terms($_POST['orderby'], 'fields=ids&order='.$_POST['order']) ).')', $sql);
$order = "ORDER BY ".$wpdb->prefix."terms.name ".$_POST['order'];
$sql = str_replace("[orderby]", $order, $sql);

} elseif (strpos($sql, "SELECT SQL_CALC_FOUND_ROWS ".$wpdb->prefix."posts.* FROM ".$wpdb->prefix."posts WHERE 1=1 ") !== false ){

$replace = "

SELECT SQL_CALC_FOUND_ROWS ".$wpdb->prefix."terms.name, ".$wpdb->prefix."posts.* FROM ".$wpdb->prefix."posts
INNER JOIN ".$wpdb->prefix."term_relationships ON (".$wpdb->prefix."posts.ID = wp_term_relationships.object_id)
INNER JOIN ".$wpdb->prefix."term_taxonomy ON (".$wpdb->prefix."term_relationships.term_taxonomy_id = ".$wpdb->prefix."term_taxonomy.term_taxonomy_id)
INNER JOIN ".$wpdb->prefix."terms ON (".$wpdb->prefix."terms.term_id IN (".implode(',', get_terms($_POST['orderby'], 'fields=ids&order='.$_POST['order']) ).") AND ".$wpdb->prefix."terms.term_id = ".$wpdb->prefix."term_taxonomy.term_id )
WHERE
";
$sql = str_replace("SELECT SQL_CALC_FOUND_ROWS ".$wpdb->prefix."posts.* FROM ".$wpdb->prefix."posts WHERE", $replace, $sql);
$sql = str_replace("ORDER BY ".$wpdb->prefix."posts.post_date DESC", "[orderby]", $sql);
$order = "GROUP BY ".$wpdb->prefix."posts.ID ORDER BY ".$wpdb->prefix."terms.name ".$_POST['order'];
$sql = str_replace("[orderby]", $order, $sql);

}

return $sql;
}