WP_QUERY WITH“POST_TITLE LIKE‘SOURCE%’”?

时间:2011-05-30 作者:Ludo

我需要做一个WP_Query 使用LIKEpost_title.

我从这个普通的WP_Query:

$wp_query = new WP_Query( 
    array (
        \'post_type\'        => \'wp_exposants\',
        \'posts_per_page\'   => \'1\',
        \'post_status\'      => \'publish\',
        \'orderby\'          => \'title\', 
        \'order\'            => \'ASC\',
        \'paged\'            => $paged
    )
); 
但在SQL中,我实际上想做的是:

$query = "
        SELECT      *
        FROM        $wpdb->posts
        WHERE       $wpdb->posts.post_title LIKE \'$param2%\'
        AND         $wpdb->posts.post_type = \'wp_exposants\'
        ORDER BY    $wpdb->posts.post_title
";
$wpdb->get_results($query);
输出打印我正在查询的结果,但我使用常规<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> 显示结果
这不适用于$wpdb->get_results().

如何实现我在这里描述的目标?

5 个回复
最合适的回答,由SO网友:Jan Fabry 整理而成

我会打开过滤器来解决这个问题WP_Query. 一种检测额外查询变量并将其用作标题前缀的查询变量。

add_filter( \'posts_where\', \'wpse18703_posts_where\', 10, 2 );
function wpse18703_posts_where( $where, &$wp_query )
{
    global $wpdb;
    if ( $wpse18703_title = $wp_query->get( \'wpse18703_title\' ) ) {
        $where .= \' AND \' . $wpdb->posts . \'.post_title LIKE \\\'\' . esc_sql( $wpdb->esc_like( $wpse18703_title ) ) . \'%\\\'\';
    }
    return $where;
}
这样你仍然可以打电话WP_Query, 您只需将标题作为wpse18703_title 参数(或将名称更改为较短的名称)。

SO网友:Nagendra Rao

Simplified:

function title_filter( $where, &$wp_query )
{
    global $wpdb;
    // 2. pull the custom query in here:
    if ( $search_term = $wp_query->get( \'search_prod_title\' ) ) {
        $where .= \' AND \' . $wpdb->posts . \'.post_title LIKE \\\'%\' . esc_sql( like_escape( $search_term ) ) . \'%\\\'\';
    }
    return $where;
}

$args = array(
    \'post_type\' => \'product\',
    \'posts_per_page\' => $page_size,
    \'paged\' => $page,
    // 1. define a custom query var here to pass your term through:
    \'search_prod_title\' => $search_term,
    \'post_status\' => \'publish\',
    \'orderby\'     => \'title\', 
    \'order\'       => \'ASC\'
);

add_filter( \'posts_where\', \'title_filter\', 10, 2 );
$wp_query = new WP_Query($args);
remove_filter( \'posts_where\', \'title_filter\', 10 );
return $wp_query;
SO网友:Ashan Jay

希望更新您为wordpress 4.0及以上版本编写的代码,因为esc\\u sql()在4.0以上版本中已被弃用。

function title_filter($where, &$wp_query){
    global $wpdb;

    if($search_term = $wp_query->get( \'search_prod_title\' )){
        /*using the esc_like() in here instead of other esc_sql()*/
        $search_term = $wpdb->esc_like($search_term);
        $search_term = \' \\\'%\' . $search_term . \'%\\\'\';
        $where .= \' AND \' . $wpdb->posts . \'.post_title LIKE \'.$search_term;
    }

    return $where;
}
其余的都是一样的。

我还想指出,您可以使用s WP\\u Query参数中的变量传递搜索词,我相信这也会搜索文章标题。

像这样:

$args = array(
    \'post_type\' => \'post\',
    \'s\' => $search_term,
    \'post_status\' => \'publish\',
    \'orderby\'     => \'title\', 
    \'order\'       => \'ASC\'        
);
$wp_query = new WP_Query($args);

SO网友:Christine Cooper

在这里发布了一些易受攻击的解决方案,我提供了一个经过简化和净化的版本。

首先,我们为posts_where 过滤器,允许您仅显示符合特定条件的帖子:

function cc_post_title_filter($where, &$wp_query) {
    global $wpdb;
    if ( $search_term = $wp_query->get( \'cc_search_post_title\' ) ) {
        $where .= \' AND \' . $wpdb->posts . \'.post_title LIKE \\\'%\' . $wpdb->esc_like( $search_term ) . \'%\\\'\';
    }
    return $where;
}
现在我们添加cc_search_post_title 进入我们的查询参数:

$args = array(
    \'cc_search_post_title\' => $search_term, // search post title only
    \'post_status\' => \'publish\',
);
最后,将过滤器包装在查询周围:

add_filter( \'posts_where\', \'cc_post_title_filter\', 10, 2 );
$query = new WP_Query($args);
remove_filter( \'posts_where\', \'cc_post_title_filter\', 10 );
使用get\\u posts()某些检索帖子的函数不会运行过滤器,因此附加的posts\\u过滤器函数不会修改查询。如果您计划使用get_posts() 要查询您的帖子,您需要设置suppress_filters 要在参数数组中为false,请执行以下操作:

$args = array(
    \'cc_search_post_title\' => $search_term,
    \'suppress_filters\' => FALSE,
    \'post_status\' => \'publish\',
);
现在您可以使用get_posts():

add_filter( \'posts_where\', \'cc_post_title_filter\', 10, 2 );
$posts = get_posts($args);
remove_filter( \'posts_where\', \'cc_post_title_filter\', 10 );
那么s 参数Thes 参数可用:

$args = array(
    \'s\' => $search_term,
);
将搜索词添加到s 参数起作用,它将搜索帖子标题,也将搜索帖子内容。

那么title WP 4.4中添加的参数

将搜索项传递到title 参数:

$args = array(
    \'title\' => $search_term,
);
区分大小写,并且LIKE, 不%LIKE%. 这意味着搜索hello 不会返回带有标题的帖子Hello WorldHello.

SO网友:David Choy

在我之前的其他答案的基础上,为了在您希望搜索在元字段或帖子标题中包含单词的帖子时提供灵活性,我通过“title\\u filter\\u relation”参数提供该选项在这个实现中,我只允许“OR”或“AND”输入,默认值为“AND”

function title_filter($where, &$wp_query){
    global $wpdb;
    if($search_term = $wp_query->get( \'title_filter\' )){
        $search_term = $wpdb->esc_like($search_term); //instead of esc_sql()
        $search_term = \' \\\'%\' . $search_term . \'%\\\'\';
        $title_filter_relation = (strtoupper($wp_query->get( \'title_filter_relation\'))==\'OR\' ? \'OR\' : \'AND\');
        $where .= \' \'.$title_filter_relation.\' \' . $wpdb->posts . \'.post_title LIKE \'.$search_term;
    }
    return $where;
}
下面是一个非常简单的帖子类型“faq”的代码示例,其中问题是帖子标题本身:

add_filter(\'posts_where\',\'title_filter\',10,2);
$s1 = new WP_Query( array(
    \'post_type\' => \'faq\',
    \'posts_per_page\' => -1,
    \'title_filter\' => $q,
    \'title_filter_relation\' => \'OR\',
    \'post_status\' => \'publish\',
    \'orderby\'     => \'title\', 
    \'order\'       => \'ASC\',
    \'meta_query\' => array(
        \'relation\' => \'OR\',
        array(
            \'key\' => \'faq_answer\',
            \'value\' => $q,
            \'compare\' => \'LIKE\'
        )
    )
));
remove_filter(\'posts_where\',\'title_filter\',10,2);

结束

相关推荐

通过自定义分类从MySQL表返回信息

Update: 我现在正试图找到一种方法来响应自定义分类法值的元数据。我正在尝试创建一些PHP,以便在将在侧边栏中显示信息的纯文本小部件中使用。因为有些信息在站点的某些部分是相同的,而不是存储在自定义字段中,所以我尝试将其存储在MYSQL表中。我希望代码能够识别post分类法,查找表中的id字段,找到与分类法共享id的行,然后回显该行上其他字段的值。我有自己的分类设置和表格,但我找不到多少先例,因此我很难知道从哪里开始。到目前为止,我试图调用当前post分类法的值,但我无法让它在循环之外工作<?ph