【问题标题】:Wordpress: WP_Query how to apply search criteria with custom post typeWordpress:WP_Query 如何使用自定义帖子类型应用搜索条件
【发布时间】:2012-01-11 14:47:22
【问题描述】:

我有一个自定义帖子类型,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


    【解决方案1】:

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

    $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

    【讨论】:

      【解决方案2】:

      在此处传递您的搜索字符串,例如('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;
      
       ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-23
        • 1970-01-01
        • 2023-01-31
        • 2013-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-20
        相关资源
        最近更新 更多