我假设,它应该类似于在税务查询中对 category_name 或 category_in 运算符使用 LIKE 子句。
$args = get_posts(array(
'字段' => 'id',
'post_type' => 'post',
'post_status' => '发布',
'posts_per_page' => -1,
's' => $_REQUEST['s'] ? $_REQUEST['s'] : '' ,
'tax_query' => 数组(
大批(
'分类法' => '分类法名称',
'field' => 'slug',
'terms' => 'SLUG OF the TERM', // LIKE(这里应该是任何 LIKE 子句等)
),
)
));
如何实现这个场景,意味着当用户输入任何与类别名称匹配的关键字时,它应该从该类别中提取所有结果以及一般搜索结果。
示例:用户在搜索栏中输入“ ABC ”,并且存在名称为“ ABC Park ”的可用类别,那么它应该从该类别中提取结果以及具有包含“ ABC”的帖子标题和内容的结果。
好的...我想出了一个解决方案,我terms用 LIKE 查询从表中获取了所有类别 id ,然后在其他查询中将此数组作为类别参数传递。这是代码。
$term_ids=array();
$cat_Args="SELECT * FROM $wpdb->terms WHERE name LIKE '%".$_REQUEST['s']."%' ";
$cats = $wpdb->get_results($cat_Args, OBJECT);
array_push($term_ids,$cats[0]->term_id);
$q1 = get_posts(array(
'fields' => 'ids',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
's' => $_REQUEST['s'] ? $_REQUEST['s'] : ''
));
$q2 = get_posts(array(
'fields' => 'ids',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'category' =>$term_ids
));
$unique = array_unique( array_merge( $q1, $q2 ) );
$posts = get_posts(array(
'post_type' => 'post',
'post__in' => $unique,
'posts_per_page' => -1
));
if ($posts ) :
foreach( $posts as $post ) :
//show results
endforeach;
endif;
Run Code Online (Sandbox Code Playgroud)
但我仍然想要一种更简单和精确的方式。:)
| 归档时间: |
|
| 查看次数: |
6168 次 |
| 最近记录: |