我需要根据帖子标题和元值过滤帖子内容 [英] I need filter content of posts accoring to post title and meta value

查看:25
本文介绍了我需要根据帖子标题和元值过滤帖子内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已应用以下代码.

$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...

推荐答案

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

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

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

now add search_title into your wp_query argument

作为:

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

这篇关于我需要根据帖子标题和元值过滤帖子内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆