在计划显示表单的任何位置输入表单
/**** The Form ****/
<form name="searchcars" method="get" action="http://yourdomain.com/your-page-template-slug">
<label>Search</label>
<input type="text" name="searchfor">
<label>Location</label>
<Select name="location">
<?php
$locations = get_terms(\'locations\');
foreach($locations as $location)
{
?>
<option value="<?php echo $location->slug; ?>"><?php echo $location->name; ?></option>
<?php
}
?>
</select>
<button type="select" name="search">Search</button>
</form>
创建一个新的文件模板,并将其分配给一个页面,其中的slug将用作上述表单的操作。
<?php
/*
*Template Name: Custom Search
*/
$searchterm = isset($_GET[\'searchfor\']) ? $_GET[\'searchfor\'] : \'\';
$location = isset($_GET[\'location\']) ? $_GET[\'location\'] : \'\';
$query = new WP_Query(array(
\'post_type\' => \'...\', /** Replace ... with the name of your custom post type **/
\'location\' => $location, /** Replace the \'location\' with \'<name-of-your-taxonomy>\' **/
\'posts_per_page\' => 10,
\'like\' => $searchterm /** This is what we have added with the custom filter **/
)) ;
while($query->have_posts()) : $query->the_post();
/*output the results **/
endwhile;
?>
将其输入到函数文件中,以将like参数作为数组选项添加到wordpress查询中。
<?php
/** Like Filter ***/
add_filter( \'posts_where\', \'wpse18703_posts_where\', 10, 2 );
function wpse18703_posts_where( $where, &$wp_query )
{
global $wpdb;
if ( $wpse18703_title = $wp_query->get( \'like\' ) ) {
$where .= \' AND \' . $wpdb->posts . \'.post_title LIKE \\\'\' . esc_sql( like_escape( $wpse18703_title ) ) . \'%\\\'\';
}
return $where;
}
?>
注释参考:
WP_Query with "post_title LIKE 'something%'"?