Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

244
Vistas
How to Search posts using LIKE for Category Name in wordpress?
  • I need to customize the word press basic search filter.
  • It is working fine searching against keywords in post title and post content.
  • Now i need to show results where if user enters the name matching the category name, then it should pull results from that category as well as other results.
  • I am assuming, it should be something like using LIKE clause for the category_name or category_in operators in tax query.

    
    
    $args = get_posts(array(
                'fields' => 'ids',
                'post_type' => 'post',
                'post_status' => 'publish',
                'posts_per_page' => -1,
                's' =>  $_REQUEST['s'] ? $_REQUEST['s'] : '' ,
                'tax_query' => array(
                     array(
                    'taxonomy' => 'NAME OF TAXONOMY',
                    'field'    => 'slug',
                    'terms'    => 'SLUG OF the TERM', // LIKE (here should be any LIKE clause etc)
                     ),
                )   
            )); 
     

    How to achieve this scenario, means when user enters any keyword matching the category name it should pull all the results from that category along with general search results.

Example: In search bar user writes "ABC" and there is category available with name "ABC Park", then it should pull results from this category along with results having post titles and content which contain "ABC".

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Okay... I come up with a solution, I've fetched all category ids from table terms with a LIKE query, then in other query passed this array as a category parameter. Here is the code.

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

But Still i would like a more minimal and precise way. :)

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda