Display posts by tags

Display posts only with a tag, and sort alphabetical first by tag and then by post.

  1. Get tags used only by custom post type links.
  2. Only output name of the tag if it has a post attached to it.
  3. For each tag name output all the posts (title and special field type notes).
        $tags = get_tags();
        foreach ( $tags as $tag )
            $tag_query = new WP_Query( array(
                'post_type' => array('links'),
                'tag_id' => $tag->term_id,
                'posts_per_page' => -1,
                'orderby' => 'title',
                'order' => 'ASC',
                'no_found_rows' => true,
                ) );
            if( $tag_query->have_posts() )
                    echo '<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">';
                        echo '<h3>' .$tag->name. '</h3>';
                    echo '</div><!-- .col -->';
        while ( $tag_query->have_posts() ) : $tag_query->the_post(); ?>
            <div class="col-lg-3 col-md-4 col-sm-6 col-xs-12"><!-- link -->
                <a href="<?php echo types_render_field("links-url", array("output"=>"raw")); ?>">
                    <p><?php the_title(); ?>
                        <?php if(types_render_field('links-notes', array('raw'=>'true'))){ ?>
                            <span>(<?php echo types_render_field("links-notes", array("output"=>"HTML")); ?>)</span>
                    <?php } ?>
            </div><!-- .col -->
             <?php endwhile; } ?>
<?php wp_reset_postdata(); ?>

The output result:


Leave a Reply

Your email address will not be published. Required fields are marked *