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

问题描述 投票:6回答:2

我有一个自定义的帖子类型,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的新手,甚至不知道我是否在问一个愚蠢的问题。但我真的很困惑,需要一个解决方案。任何帮助将不胜感激。谢谢大家。

php wordpress custom-post-type
2个回答
10
投票

将“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


1
投票

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

 ?>
© www.soinside.com 2019 - 2024. All rights reserved.