php - Exclude posts with specific metadata from search?

I have created this searchfunction that looks like this:function search_data_fetch() {$search_query = new WP_Query( arra

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 badges
Add a comment  | 

1 Answer 1

Reset to default 2

You 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745596987a4635175.html

相关推荐

  • php - Exclude posts with specific metadata from search?

    I have created this searchfunction that looks like this:function search_data_fetch() {$search_query = new WP_Query( arra

    12小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信