有一些教程解释了如何将搜索限制为特定类别。
我的问题是,有没有办法将 wordpress 的搜索配置为在自定义帖子类型中搜索自定义字段值。
因此,例如,如果我搜索“hello”,结果会出现具有某个自定义字段等于“hello”的帖子。某个帖子也将是某个自定义帖子类型。
任何帮助表示赞赏。
有一些教程解释了如何将搜索限制为特定类别。
我的问题是,有没有办法将 wordpress 的搜索配置为在自定义帖子类型中搜索自定义字段值。
因此,例如,如果我搜索“hello”,结果会出现具有某个自定义字段等于“hello”的帖子。某个帖子也将是某个自定义帖子类型。
任何帮助表示赞赏。
要按自定义帖子类型过滤搜索,请使用:
<?php query_posts($query_string . '&post_type=custom-post-type-name' ); ?>
在循环之前.. 然后在循环内添加类似于此的条件
<?php if ($meta_data[ 'meta-name' ] == 'hello') {
//do something
} ?>
我想这就是你要找的东西:
键是自定义字段。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();