admin管理员组文章数量:1024582
I have created this searchfunction that looks like this:
function search_data_fetch() {
$search_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'visitor' ) );
if ( $search_query->have_posts() ) {
while( $search_query->have_posts() ) {
$search_query->the_post();
?>
<div class="module">
<span class="infos-name"><?php the_title(); ?></span>
<span class="infos-nick"><?php echo the_content(); ?></span>
</div>
<?php
}
wp_reset_postdata();
}
else {
?>
<div id="nothing_found">
<p>Nothing found</p>
</div>
<?php
}
die();
}
add_action('wp_ajax_data_fetch', 'search_data_fetch');
add_action('wp_ajax_nopriv_data_fetch', 'search_data_fetch');
And the posts I search for with the custom post type visitor
all have a custom meta data field that is called visitor-hide
and when it is set to yes
I want it to not be visible in the search at all.
So how do I exclude posts which have the meta data visitor-hide
set to yes
from the search results while showing every other post with this post type.
Thank you!
I have created this searchfunction that looks like this:
function search_data_fetch() {
$search_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'visitor' ) );
if ( $search_query->have_posts() ) {
while( $search_query->have_posts() ) {
$search_query->the_post();
?>
<div class="module">
<span class="infos-name"><?php the_title(); ?></span>
<span class="infos-nick"><?php echo the_content(); ?></span>
</div>
<?php
}
wp_reset_postdata();
}
else {
?>
<div id="nothing_found">
<p>Nothing found</p>
</div>
<?php
}
die();
}
add_action('wp_ajax_data_fetch', 'search_data_fetch');
add_action('wp_ajax_nopriv_data_fetch', 'search_data_fetch');
And the posts I search for with the custom post type visitor
all have a custom meta data field that is called visitor-hide
and when it is set to yes
I want it to not be visible in the search at all.
So how do I exclude posts which have the meta data visitor-hide
set to yes
from the search results while showing every other post with this post type.
Thank you!
Share Improve this question asked Apr 12, 2019 at 10:37 jockebqjockebq 4631 gold badge6 silver badges18 bronze badges1 Answer
Reset to default 2You need an additional parameter meta_query
in the WP_Query
arguments.
$query_args = [
'posts_per_page' => -1,
's' => esc_attr( $_POST['keyword'] ),
'post_type' => 'visitor',
'meta_query' => [
'key' => 'visitor-hide',
'value' => 'yes',
'compare' => '!=',
]
]
or in cases like yours (query by single custom field):
$query_args = [
'posts_per_page' => -1,
's' => esc_attr( $_POST['keyword'] ),
'post_type' => 'visitor',
'key' => 'visitor-hide',
'value' => 'yes',
'compare' => '!=',
]
You can find more about custom field parameters in WP_Query
here.
I have created this searchfunction that looks like this:
function search_data_fetch() {
$search_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'visitor' ) );
if ( $search_query->have_posts() ) {
while( $search_query->have_posts() ) {
$search_query->the_post();
?>
<div class="module">
<span class="infos-name"><?php the_title(); ?></span>
<span class="infos-nick"><?php echo the_content(); ?></span>
</div>
<?php
}
wp_reset_postdata();
}
else {
?>
<div id="nothing_found">
<p>Nothing found</p>
</div>
<?php
}
die();
}
add_action('wp_ajax_data_fetch', 'search_data_fetch');
add_action('wp_ajax_nopriv_data_fetch', 'search_data_fetch');
And the posts I search for with the custom post type visitor
all have a custom meta data field that is called visitor-hide
and when it is set to yes
I want it to not be visible in the search at all.
So how do I exclude posts which have the meta data visitor-hide
set to yes
from the search results while showing every other post with this post type.
Thank you!
I have created this searchfunction that looks like this:
function search_data_fetch() {
$search_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'visitor' ) );
if ( $search_query->have_posts() ) {
while( $search_query->have_posts() ) {
$search_query->the_post();
?>
<div class="module">
<span class="infos-name"><?php the_title(); ?></span>
<span class="infos-nick"><?php echo the_content(); ?></span>
</div>
<?php
}
wp_reset_postdata();
}
else {
?>
<div id="nothing_found">
<p>Nothing found</p>
</div>
<?php
}
die();
}
add_action('wp_ajax_data_fetch', 'search_data_fetch');
add_action('wp_ajax_nopriv_data_fetch', 'search_data_fetch');
And the posts I search for with the custom post type visitor
all have a custom meta data field that is called visitor-hide
and when it is set to yes
I want it to not be visible in the search at all.
So how do I exclude posts which have the meta data visitor-hide
set to yes
from the search results while showing every other post with this post type.
Thank you!
Share Improve this question asked Apr 12, 2019 at 10:37 jockebqjockebq 4631 gold badge6 silver badges18 bronze badges1 Answer
Reset to default 2You need an additional parameter meta_query
in the WP_Query
arguments.
$query_args = [
'posts_per_page' => -1,
's' => esc_attr( $_POST['keyword'] ),
'post_type' => 'visitor',
'meta_query' => [
'key' => 'visitor-hide',
'value' => 'yes',
'compare' => '!=',
]
]
or in cases like yours (query by single custom field):
$query_args = [
'posts_per_page' => -1,
's' => esc_attr( $_POST['keyword'] ),
'post_type' => 'visitor',
'key' => 'visitor-hide',
'value' => 'yes',
'compare' => '!=',
]
You can find more about custom field parameters in WP_Query
here.
本文标签: phpExclude posts with specific metadata from search
版权声明:本文标题:php - Exclude posts with specific metadata from search? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745596984a2158230.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论