include tag and category in wordpress search results

Asked

Viewed 1,493 times

2

Hello, I would like to include tags and categories in the search results ie, when typing in the form the category HAIR would like to appear all posts of this category.

I was using the code below:( But I realized that the pages are missing from the search results).

I’m new with wp for 3 days looking for this resolution, in case anyone has any tips I really appreciate.

Follows the code :

function atom_search_where($where){
  global $wpdb;
  if (is_search())
    $where .= "OR (t.name LIKE '%".get_search_query()."%' AND {$wpdb->posts}.post_status = 'publish')";
  return $where;
}

function atom_search_join($join){
  global $wpdb;
  if (is_search())
    $join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id";
  return $join;
}

function atom_search_groupby($groupby){
  global $wpdb;


  $groupby_id = "{$wpdb->posts}.ID";
  if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby;


  if(!strlen(trim($groupby))) return $groupby_id;


  return $groupby.", ".$groupby_id;
}

add_filter('posts_where','atom_search_where');
add_filter('posts_join', 'atom_search_join');
add_filter('posts_groupby', 'atom_search_groupby');
  • But Wordpress no longer does that? I don’t understand what the problem is.

  • OMG, you saved my life with this plugin "search Everything" I love you

2 answers

1

I don’t know why this question came to me today, but it has over 200 views and there’s no answer to something reasonably simple that’s very useful, so here it goes:

No need to use $wpdb and tamper with SQL clauses for this, just use the hook pre_get_posts and change the main query:

<?php
add_action( 'pre_get_posts', 'modifica_busca' );

function modifica_busca( $query ) {

    // pre_get_posts é chamada diversas vezes numa requisição
    // então precisamos conferir se estamos na chamada pricipal da busca
    if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
        $termo_de_busca = get_search_query();

        // busca se a palavra digitada confere com alguma categoria
        // taxonomy pode ser trocada por post_tag ou outra taxonomia
        $taxonomy = 'category';
        $cat = get_term_by( 'slug', $termo_de_busca, $taxonomy );

        // se o termo de busca não existe como termo de uma 
        // taxonomia, retorna e finaliza a busca aqui
        if ( ! $cat ) {
            return;
        }

        // remove o termo de busca pra que a busca não inclua somente
        // os posts que tem a palavra no titulo ou conteudo
        $query->set( 's', '' );

        // busca outras consultas por taxonomia que podem existir
        $tax_query = $query->get( 'tax_query' );

        // adiciona a nossa
        $tax_query[] = array(
            'field' => 'slug',
            'taxonomy' => $taxonomy,
            'terms' => $termo_de_busca,
        );

        // adiciona a busca completa de volta à consulta principal
        $query->set( 'tax_query', $tax_query );
    }
}
  • Where do I put this code ?

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.