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

1

333
Visualizações
Wordpress ajax filter posts by combining multiple taxonomies

I am working on a filter for a post grid in Wordpress with ajax.

It's fundamentally working as a whole, but I'm trying to filter using multiple taxonomies. But instead of it combining the taxonomies to refine the search such as posts tagged with 'a' AND 'b'

It's just showing all posts with the tag 'a' and with the tag 'b'

$args = array(
    'post_type' => 'projects',
    'orderby' => 'date', // we will sort posts by date
    'order' => $_POST['date'] // ASC or DESC
);

if( isset($_POST['multi_subject']) && !empty($_POST['multi_subject']) ) {
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'category',
            'field' => 'id',
            'terms' => $_POST['multi_subject']
        )
    );
}

if( isset($_POST['multi_style']) && !empty($_POST['multi_style']) ) {
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'styles',
            'field' => 'id',
            'terms' => $_POST['multi_style']
        )
    );
}

$query = new WP_Query( $args );

if( $query->have_posts() ) :
    while( $query->have_posts() ): $query->the_post();

        echo '<a href="'. get_permalink() .'" data-author="'. get_the_author() .'" data-tier="'. get_author_role() .'">';
        echo '<h2>' . $query->post->post_title . '</h2>';
        echo '<div>'. the_post_thumbnail() .'</div>';
        echo '</a>';
    endwhile;
    wp_reset_postdata();
else :
    echo 'No posts found';
endif;

die();

I'm sure it's something simple to do with the isset then setting the args but I can't figure it out.

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

0

Your current tax query does not allow for both taxonomies to be checked cause if both are set your second tax check will overrite the first. To allow for both you need to append them... refactor your tax_query to something like this:

// start with an empty array
$args['tax_query'] = array();

// check for first taxonomy
if( isset($_POST['multi_subject']) && !empty($_POST['multi_subject']) ) {
    // append to the tax array
    $args['tax_query'][] = array(
        'taxonomy' => 'category',
        'field' => 'id',
        'terms' => $_POST['multi_subject']
    );
}

// check for another taxonomy
if( isset($_POST['multi_style']) && !empty($_POST['multi_style']) ) {
    // append to the tax array
    $args['tax_query'][] = array(
        'taxonomy' => 'styles',
        'field' => 'id',
        'terms' => $_POST['multi_style']
    );
}
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