按标题搜索表单和自定义query_posts

时间:2016-06-14 23:07:12

标签: wordpress search filter

该代码只能按表单设置变量。

function.php

<?php
function title_filter( $where, &$wp_query ) {
global $wpdb;
if ( $search_term = $wp_query->get( 'search_title' ) ) {
    $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\'';
}
return $where;
}
add_filter( 'posts_where', 'title_filter', 10, 2 );
?>

form.php的

<form role="search" method="get" id="form_percorsi" action="<?php bloginfo('url'); ?>/category/percorsi/" style="float:left;margin-right:10px;">
<h1>Search</h1>
<input placeholder="Cerca…" value="" id="search_them" name="search_title" title="Search for:" type="search">
</form>

我对显示项目的查询

$search_them = $_GET['search_them'];

$args = array( 'paged' => $paged, 'orderby' => $order_by, 'order' => $order, 'search_title' => $search_them );

如果我通过非工作形式搜索标题......

有解决方案吗?

0 个答案:

没有答案