I need filter content of posts accoring to post ti

2020-01-20 02:27发布

I have applied following code.

$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>";

But showing result according to meta value not for post title. Please let me know solutions...

1条回答
Juvenile、少年°
2楼-- · 2020-01-20 03:04

you can use post title into wp_query by adding this to functions.php file:

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;
}

now add search_title into your wp_query argument

as:

$args = array('post_type' => 'product','search_title'   => $title,'meta_query' => array( array( 'key' => 'custom_field_ID_1','value' => $postcode,'compare' => 'LIKE' )));
查看更多
登录 后发表回答