Wordpress: WP_Query how to apply search criteria with custom post type

后端 未结 2 1092
梦毁少年i
梦毁少年i 2021-02-03 14:06

I have a custom post type, photo, and need to search for photos matching the title or description with the search keyword with various criteria: contains LIKE

相关标签:
2条回答
  • 2021-02-03 14:11

    Add the "s" key to your existing arguments array:

    $loop = new WP_Query( array(
        'post_type' => 'photo',
        'posts_per_page' => 12,
        'orderby' => 'post_date',
        's' => 'search_term'
    ));
    

    Documentation can be found at: http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter

    0 讨论(0)
  • 2021-02-03 14:12

    Pass your search string here example like this ( 's'=>'test' )

     <?php
    
     /*pass your search string here example like this ( 's'=>'test' ) */
     $args=array('s'=>'test','order'=> 'DESC', 'posts_per_page'=>get_option('posts_per_page'));
    
       $query=new WP_Query($args);
    
      if( $query->have_posts()): 
    
      while( $query->have_posts()): $query->the_post();
    
     {
       echo $post->post_title;
       echo $post->post_content;
     }
    
     endwhile; 
     else:
     endif;
    
     ?>
    
    0 讨论(0)
提交回复
热议问题