我需要根据帖子标题和元值过滤帖子内容

时间:2014-04-23 08:42:48

标签: wordpress wp-query

我已应用以下代码。

$postcode = $_POST['postcode'];
$title = $_POST['term'];

$args = array('post_type' => 'product','meta_query' => array( array( 'key' => 'custom_field_ID_1','value' => $postcode,'compare' => 'LIKE' )));

$the_query = new WP_Query( $args );
echo"<pre>";print_r($the_query);echo"</pre>";

但是根据元值显示结果而不是帖子标题。请让我知道解决方案...

1 个答案:

答案 0 :(得分:0)

您可以将帖子标题添加到wp_query,将其添加到functions.php文件中:

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

现在将search_title添加到您的wp_query参数

为:

$args = array('post_type' => 'product','search_title'   => $title,'meta_query' => array( array( 'key' => 'custom_field_ID_1','value' => $postcode,'compare' => 'LIKE' )));