Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
Search within a category in worpdress

I'm trying to search within a single category on wordpress using the code below

`

    $args = new WP_Query( 'cat=199' );
    if( $args->have_posts() ){

    while ( $args->have_posts() ){ $args->the_post();
        get_template_part('content', 'what-we-eat');

    } ?>

    <?php   

    }
    else{
        _e( 'Sorry, no posts matched your criteria.' );
    }

`

however, the results shows all of the posts within that category even though the search term updates with the search term used.

Any ideas where i'm going wrong?

EDIT

I've noticed then when I remove my custom query it works, but pulls in posts from all of the categories on my blog which is what I don't want. So it's a question of excluding all other categories

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Try this

<?php

global $post;
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 199 );

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
<?php endforeach; 
?>
over 4 years ago · Santiago Trujillo Relatório

0

You want standard WP search to work in only one category? Try to add this code to the functions.php of your theme:

function search_rules($query) {
  if ($query->is_search) {
    $query->set('cat','199');
  }
  return $query;
}
add_filter('pre_get_posts','search_rules');
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda