Query all posts and not repeat the same tag

I need a query to show all posts in the site but not repeat the ones with same tag, I mean only show one post with the same tag.

My current query is

    <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = array(
        'post_type' => array('post'),
        'posts_per_page' => 30,
        'paged' => $paged,
        'order' => 'ASC',
        'orderby' => 'name' 
        );
        query_posts($args);
    ?>

How can I show only post with the same tag?

Thanks!

2 Answers
2

You can try something like this:

<?php

$tags_array   = get_tags();
$news_query  = new WP_Query;

foreach ( $tags_array as $tags ) :
    $news_query->query( array(
        'cat'                 => $tags->term_id,
        'posts_per_page'      => 1,
        'no_found_rows'       => true,
        'ignore_sticky_posts' => true,
    ));

    ?>

    <h2><?php echo esc_html( $tags->name ) ?></h2>

    <?php while ( $news_query->have_posts() ) : $news_query->the_post() ?>

            <div class="post">
                <?php the_title() ?>
                <!-- do whatever you else you want that you can do in a normal loop -->
            </div>  

    <?php endwhile ?>

<?php endforeach ?>

Leave a Comment