Search only custom taxonomies

时间:2017-03-20 作者:Ronon

我创建了一个名为people. 与此同时,在这个分类法中有超过2000人可用。现在我想让搜索一个人成为可能,例如,我搜索John, 我想接待所有有这个名字的人。我有点搞不清楚如何对其进行归档,以及如何将分类添加到搜索结果中。

我使用register\\u taxonomy创建分类法

register_taxonomy(\'people\',\'post\',array(
    \'hierarchical\' => false,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'show_admin_column\' => true,
    \'update_count_callback\' => \'_update_post_term_count\',
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'people\' ),
    \'show_in_rest\' => true,
    \'rest_controller_class\' => \'WP_REST_Terms_Controller\'
));
有什么建议吗?

编辑:

SELECT SQL_CALC_FOUND_ROWS wp_posts.*
FROM   wp_posts
   LEFT JOIN wp_term_relationships
          ON ( wp_posts.id = wp_term_relationships.object_id )
   LEFT JOIN wp_term_relationships tr
          ON wp_posts.id = tr.object_id
   INNER JOIN wp_term_taxonomy tt
           ON tt.term_taxonomy_id = tr.term_taxonomy_id
   INNER JOIN wp_terms t
           ON t.term_id = tt.term_id    
WHERE  1 = 1
   AND ( wp_term_relationships.term_taxonomy_id IN ( 2, 3 ) )
   AND ( ( ( wp_posts.post_title LIKE \'%Johnny%\' )
            OR ( wp_posts.post_excerpt LIKE \'%John%\' )
            OR ( wp_posts.post_content LIKE \'%John%\' ) )
         AND ( ( wp_posts.post_title LIKE \'%John%\' )
                OR ( wp_posts.post_excerpt LIKE \'%John%\' )
                OR ( wp_posts.post_content LIKE \'%John%\' ) ) )
   AND wp_posts.post_type = \'post\'
   AND (( wp_posts.post_status = \'publish\' ))
    OR ( t.name LIKE \'%John%\' )
GROUP  BY wp_posts.id
ORDER  BY ( CASE
          WHEN wp_posts.post_title LIKE \'%John%\' THEN 1
          WHEN wp_posts.post_title LIKE \'%John%\'
               AND wp_posts.post_title LIKE \'%John%\' THEN 2
          WHEN wp_posts.post_title LIKE \'%John%\'
                OR wp_posts.post_title LIKE \'%John%\' THEN 3
          WHEN wp_posts.post_excerpt LIKE \'%John%\' THEN 4
          WHEN wp_posts.post_content LIKE \'%John%\' THEN 5
          ELSE 6
        end ),
      wp_posts.post_date DESC
LIMIT  0, 99
编辑2:看来我必须这样做

SELECT * FROM wp_terms WHERE name LIKE "%John%"
如果我对这个观点完全错误的话,我想听听你的建议。

1 个回复
SO网友:WebElaine

创建一个提交名为“name”的字段的搜索表单。在处理表单提交的文件中,

// get the "name" the visitor searched for
$term = $_POST[\'name\'];

// query: find all posts with the \'people\' taxonomy set to the "name" they searched for
$args = array(
    \'post_type\' => \'post\', // or insert your custom post type here
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'people\',
            \'field\'    => \'slug\',
            \'terms\'    => "$sterm"
        ),
    ),
);
$query = new WP_Query( $args );

// loop through results and display
if($query->have_posts()):
    while($query->have_posts()) : $query->the_post();

    // output whatever HTML you like here
    ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php

    endwhile;
endif;

相关推荐

Media searching ignored

我们的网站使用WordPress,有很多媒体文件。我们网站的媒体名称格式如下[Car brand\'s name]-[number].jpg, 例如Tesla-1.jpg 或Aston Martin-3.jpg. 因此,我们可以通过搜索文章的名称轻松找到文章的特定媒体。但突然间,我们找不到媒体。我们正在尝试搜索名称为的媒体,但搜索结果不变。(不搜索任何内容时的媒体屏幕)(搜索Aston Martin时的媒体屏幕)当然,在填充搜索文本框后,它会显示一个加载图标,但结果总是一样的。为什么会发生这种情况?更新+