WordPress:WP_Query如何应用自定义帖子类型的搜索条件 [英] Wordpress: WP_Query how to apply search criteria with custom post type

查看:102
本文介绍了WordPress:WP_Query如何应用自定义帖子类型的搜索条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义帖子类型,照片,需要使用各种条件用搜索关键字搜索与标题或描述匹配的照片:包含 LIKE%$ search_term%,以 LIKE $ search_term%等开头。我有以下查询,但这并不根据 $ search_term

  $ search_term = $ _GET [’term’];  b 
$ b

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

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

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

解决方案

在现有参数数组中添加 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


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 %$search_term%, starts with LIKE $search_term% etc. I have the following query, but this doesn't filter records according to $search_term. Please direct me to the right direction to embed this requirement with this query.

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

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

Please be nice with me, I am a newbie in Wordpress and don't even know if I am asking a foolish question. But I am really stuck with it and need a solution. Any help will be appreciated a lot. Thank you everybody.

解决方案

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

这篇关于WordPress:WP_Query如何应用自定义帖子类型的搜索条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆