最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Search only custom taxonomies

programmeradmin3浏览0评论

I created a custom taxonomy called people. In the meantime there are over 2k people available in this taxonomy. Now I want to make it possible to search for a person, e.g. I search for John, I want to receive all persons with that name. I'm a little bit confused how to archieve this and how to add the taxonomy to the search results.

I create the taxonomy with register_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'
));

Any suggestions?

EDIT:

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

EDIT 2: It seems like I have to do sth. like this

SELECT * FROM wp_terms WHERE name LIKE "%John%"

If I'm totally wrong with this opinion, i would like to here your suggestions.

I created a custom taxonomy called people. In the meantime there are over 2k people available in this taxonomy. Now I want to make it possible to search for a person, e.g. I search for John, I want to receive all persons with that name. I'm a little bit confused how to archieve this and how to add the taxonomy to the search results.

I create the taxonomy with register_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'
));

Any suggestions?

EDIT:

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

EDIT 2: It seems like I have to do sth. like this

SELECT * FROM wp_terms WHERE name LIKE "%John%"

If I'm totally wrong with this opinion, i would like to here your suggestions.

Share Improve this question edited Mar 21, 2017 at 16:09 Ronon asked Mar 20, 2017 at 21:24 RononRonon 2291 gold badge2 silver badges7 bronze badges 6
  • You create a taxonomy through register_post_type()? Check the parameters: public and exclude_from_search – TrubinE Commented Mar 20, 2017 at 21:49
  • I use register_taxonomy to create the taxonomy codex.wordpress/Function_Reference/register_taxonomy . I didn't see an option to exlude / include it to search. – Ronon Commented Mar 20, 2017 at 21:53
  • What are the parameters public and exclude_from_search ? – TrubinE Commented Mar 20, 2017 at 21:53
  • I updated the post with the parameters. Everything else is set to default – Ronon Commented Mar 20, 2017 at 21:56
  • Look at this question: link – TrubinE Commented Mar 20, 2017 at 22:02
 |  Show 1 more comment

1 Answer 1

Reset to default 1

Create a search form that submits a field called "name". In the file that handles form submissions,

// 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;

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论