代码之家  ›  专栏  ›  技术社区  ›  E.Owen

在WordPress分类搜索中搜索子项时包含父项

  •  1
  • E.Owen  · 技术社区  · 6 年前

    我的问题是: 如何设置查询,以便在查询父项时搜索包含子项,在查询子项时搜索包含父项?

    我正在使用以下代码成功地搜索某一类型的文章,这两个词都是a 术语B。

    $args = array (
        'post_type' => 'profiles',
        'tax_query' => array(                           //(array) - use taxonomy parameters (available with Version 3.1).
            'relation' => 'AND',                        //(string) - Possible values are 'AND' or 'OR' and is the equivalent of ruuning a JOIN for each taxonomy
            array(
                'taxonomy' => 'skills',                 //(string) - Taxonomy.
                'field' => 'id',                        //(string) - Select taxonomy term by ('id' or 'slug')
                'terms' => array( (int)$skill_id ),     //(int/string/array) - Taxonomy term(s).
                'include_children' => true,             //(bool) - Whether or not to include children for hierarchical taxonomies. Defaults to true.
                'operator' => 'IN'                      //(string) - Operator to test. Possible values are 'IN', 'NOT IN', 'AND'.
            ),
            array(
                'taxonomy' => 'towns-counties',
                'field' => 'id',
                'terms' => array( (int)$area_id ),
                'include_children' => true,
                'operator' => 'IN'
            )
        )
    );
    
    $the_query = new WP_Query( $args );
    // The Loop
    if ( $the_query->have_posts() ) :
        // Do Stuff
    

    术语A是一种技能或行业,例如:建筑商、理发师、网络开发人员。

    术语B是英国境内的一个地方、一个城镇或县,例如:阿伯丁郡、利物浦、利文斯顿等。

    术语A是 等级制的术语B 等级制的

    如果最终用户搜索一个县,搜索结果将包含该县及其子县(城镇)内的所有帖子。

    0 回复  |  直到 6 年前