Wp_query multiple, without repeating category

0

I have a wp_query that filters three categories of cars, clothing and music, the order is in random.

I would like the result to change 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>
    
asked by anonymous 21.10.2016 / 04:21

1 answer

2

What you want is impossible using a query only. The best way to do it will depend on your actual situation, for example, can they always be in the same order? Type 1 car, 1 clothing, 1 music, etc? One 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' ) );
    
21.10.2016 / 14:03