Multiple wp_query without repeating category

Asked

Viewed 62 times

0

I have a wp_query that filters three categories cars, clothes and music, the order is in Random.

I would like the result to alternate without repeating the category.

        <ul>
            <?php 

            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

            $args = array(
               'category_name' => 'carros, roupas, musica',
               'orderby' => 'rand',
               'paged' => $paged,
               'posts_per_page' => 6
            );

            $wp_query = new WP_Query($args);
            if($wp_query->have_posts()):
                while($wp_query->have_posts()):
                         $wp_query->the_post();
            ?>

            <li>
                <?php
                    $thumb_id = get_post_thumbnail_id();
                    $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
                    $thumb_url = $thumb_url_array[0];
                ?>  

                <figure class="fotos-secao">
                    <a class="chocolat-image" href="<?php echo $thumb_url; ?>"  title ='<?php the_title_attribute(); ?>'>
                        <img src="<?php echo $thumb_url; ?>" alt="" class="img_gallery">
                    </a>            
                </figure>
            </li>

            <?php endwhile; ?>

            <?php wp_reset_postdata(); ?>

            <?php else: ?>
                <p>Não entendi o que procura.</p>
            <?php endif; ?>
        </ul>

1 answer

2

What you want is impossible using a query only. The best way to do it will depend on your real situation, for example, can they always be in the same order? Type 1 car, 1 outfit, 1 song, etc? A possible implementation is as follows:

    $args = array(
       'orderby' => 'rand',
       'paged' => $paged,
       'posts_per_page' => 6,
       'fields' => 'ids',
       'no_found_rows' => true,
       'update_post_meta_cache' => false,
       'update_post_term_cache' => false,
    );

    // Pega ids de 6 carros aleatórios
    $args['category_name'] = 'carros';
    $carros = new WP_Query($args)
    $carros = $carros->get_posts();

    // Pega ids de 6 roupas aleatórias
    $args['category_name'] = 'roupas';
    $roupas = new WP_Query($args);
    $roupas = $roupas->get_posts();

    // Pega ids de 6 musicas aleatórias
    $args['category_name'] = 'musica';
    $musica = new WP_Query($args);
    $musica = $musica->get_posts();

    // Junta os ids em uma lista
    $todos = array_merge( $carros, $roupas, $musica );

    // Embaralha a lista para nao repetir categorias
    for( $i = 0; $i < 6; $i++ ) {
        $embaralhado[] = $todos[$i];
        $embaralhado[] = $todos[$i+6];
        $embaralhado[] = $todos[$i+12];
    }

    // Busca os posts da lista na ordem que ficaram embaralhados
    $wp_query = WP_Query( array( 'post__in' => $embaralhado, 'orderby' => 'post__in' ) );

Browser other questions tagged

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