admin管理员组文章数量:1332890
I'm trying to exclude al posts with certain meta data from the search results but so far it's not working. Here's the code I'm using:
function ZoekGeenLegeItems( $query ) {
if( is_admin() || !$query->is_search() || $query->get( 'post_type' ) != 'item' )
return $query;
$query->set('meta_query', array(
'relation' => 'OR',
array(
'key' => 'KEY_itm_leeg'
,'value' => '0'
)
));
return $query;
}
add_filter('pre_get_posts','ZoekGeenLegeItems');
Any ideas, anyone?
I'm trying to exclude al posts with certain meta data from the search results but so far it's not working. Here's the code I'm using:
function ZoekGeenLegeItems( $query ) {
if( is_admin() || !$query->is_search() || $query->get( 'post_type' ) != 'item' )
return $query;
$query->set('meta_query', array(
'relation' => 'OR',
array(
'key' => 'KEY_itm_leeg'
,'value' => '0'
)
));
return $query;
}
add_filter('pre_get_posts','ZoekGeenLegeItems');
Any ideas, anyone?
Share Improve this question asked Apr 22, 2016 at 10:20 JasperJasper 11 bronze badge1 Answer
Reset to default 1First, the pre_get_posts
hook is an action and not a filter.
Then the meta_query
parameter should contain the relation
only if there is more than one inner meta_query array (codex).
Finally you do not need to return the $query
argument as it is passed by reference to your callback function.
Your code should look like this:
function ZoekGeenLegeItems( $query ) {
if( is_admin() || !$query->is_search() || $query->get( 'post_type' ) != 'item' )
return;
$query->set('meta_query', array(
array(
'key' => 'KEY_itm_leeg',
'value' => '0'
)
) );
}
add_action('pre_get_posts','ZoekGeenLegeItems');
本文标签: Exclude posts with certain meta data from search results
版权声明:本文标题:Exclude posts with certain meta data from search results 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742307685a2450245.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论