使用自定义帖子类型搜索自定义字段

时间:2011-08-10 06:06:32

标签: wordpress search custom-post-type custom-fields

有一些教程可以解释如何将搜索限制为特定类别。

我的问题是,有没有办法在自定义帖子类型中配置wordpress'搜索,搜索自定义字段值。

因此,例如,如果我搜索“hello”,结果会出现具有某个自定义字段等于“hello”的帖子。某个帖子也是某种自定义帖子类型。

感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

要按自定义帖子类型过滤搜索:

<?php query_posts($query_string . '&post_type=custom-post-type-name' ); ?>
在循环之前

然后在循环内添加一个类似于

的条件
<?php if ($meta_data[ 'meta-name' ] == 'hello') {
    //do something
} ?>

答案 1 :(得分:0)

我认为这就是你要找的东西:

键是自定义字段。 value是您要查找的值 和compare是您要使用的运算符。 如果你愿意,你也可以使用LIKE。

// WP_Query arguments
$args = array (
    'post_type'              => 'vendors',
    'post_status'            => 'published',
    'meta_query'             => array(
        array(
            'key'       => 'state',
            'value'     => 'Misissipi',
            'compare'   => '=',
        ),
    ),
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // do something
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();
相关问题