I need filter content of posts accoring to post title and meta value

眉间皱痕 提交于 2019-12-14 00:02:20

问题


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:


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' )));


来源:https://stackoverflow.com/questions/23239118/i-need-filter-content-of-posts-accoring-to-post-title-and-meta-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!