繁体   English   中英

Wordpress:WP_Query如何应用自定义帖子类型的搜索条件

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

我有一个自定义的帖子类型, photo ,并且需要搜索与标题或说明匹配的照片以及具有各种标准的搜索关键字:包含LIKE %$search_term% ,以LIKE $search_term%等开头。我有以下查询,但是这不会根据$search_term过滤记录。 请指导我使用此查询嵌入此要求的正确方向。

$search_term = $_GET['term'];
$search_criteria = $_GET['type'];

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

请跟我好,我是Wordpress的新手,甚至不知道我是否在问一个愚蠢的问题。 但我真的很困惑,需要一个解决方案。 任何帮助将不胜感激。 谢谢大家。

将“s”键添加到现有的arguments数组中:

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

有关文档,请访问: http//codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter

在这里传递您的搜索字符串示例如下('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;

 ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM