如何使用自定义wp_query args实现LIKE%x,%x%,x%
答案 0 :(得分:0)
query_posts(array('post_type'=>'photo'));
add_filter( 'posts_where', 'layzend_posts_where', 10, 2 );
function layzend_posts_where( $where, &$wp_query )
{
global $wpdb;
$term = $_GET['term'] ? $_GET['term'] : '';
$type = $_GET['type'] ? $_GET['type'] : '';
if ( !empty($term) )
{
switch ($type)
{
case 'all':
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $term ) ) . '%\'';
break;
case 'any':
$terms = explode(' ', $term);
$where .= ' AND ';
foreach ($terms as $t)
{
$whereed[] = $wpdb->posts . ".post_title LIKE '%$t%' ";
}
$where .= '(' . implode(' OR ',$whereed) . ')';
break;
case 'id':
$where .= ' AND ' . $wpdb->posts . '.ID = \'' . esc_sql( like_escape( $term ) ) . '\'';
break;
case 'exact':
$where .= ' AND ' . $wpdb->posts . '.post_title = \'' . esc_sql( like_escape( $term ) ) . '\'';
break;
}
}
return $where;
}
?>
<?php if ( have_posts() ): ?>
<?php
$paged = get_url_var('page');
$loop = new WP_Query( array(
'post_type' => 'photo',
'posts_per_page' => 12,
'orderby'=> 'post_date',
'' => '',
'paged'=>$paged
));