如何在 wordpress 中使用 LIKE 作为类别名称搜索帖子?

Esa*_*smi 8 php wordpress

  • 我需要自定义 wordpress 基本搜索过滤器。
  • 它可以很好地搜索帖子标题和帖子内容中的关键字。
  • 现在我需要显示结果,如果用户输入与类别名称匹配的名称,那么它应该从该类别以及其他结果中提取结果。
  • 我假设,它应该类似于在税务查询中对 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”的帖子标题和内容的结果。

Esa*_*smi 6

好的...我想出了一个解决方案,我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)

但我仍然想要一种更简单和精确的方式。:)