1

How to query posts from categories with specific tags? For Example if I visit "localhost/tag/tutorial" the archive page would show all post from all category with tutorial tag.

But the view would be seperated by category. For example:

Category 1

  • Category 1 Post 1 With tutorial tag
  • Category 1 Post 2 with tutorial tag

Category 2

  • Category 2 Post 2 With tutorial tag
  • Category 2 Post 10 With tutorial tag

NB: The posts show in each categories with same tags.

I have a code that show latest posts in each categories

<?php
        $categories = get_categories();
        foreach($categories as $category) { ?>

        <div class="CategoryPost">
          <h2><?php echo $category->name; ?></h2>
          <ul>
          <?php
            $post_args = array(
              'category' => $category->term_id,
              'numberposts' => 9999,
              'orderby'=> 'post_date',
              'order' => 'ASC',
              'post_type' => 'post'
            );

            $posts = get_posts($post_args);
            foreach($posts as $post) { ?>
              <li id="post-<?php the_ID(); ?>"><span><?php the_time('d M Y') ?></span> <i class="Seperate">&raquo;</i> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php } ?>
            </ul>
          </div>
        <?php }
        wp_reset_postdata(); ?>

The code above would show latest posts in each category with this look.

Category 1 Name

  • Category 1 Post 1
  • Category 1 Post 2

Category 2 Name

  • Category 1 Post 1
  • Category 1 Post 2

Category 3 Name

  • Category 1 Post 1
  • Category 1 Post 2

I would like to make a tags archive that work with that template and the posts would be shown in each categories even the posts have the same tags.

9
  • I hope here you will find your answer wordpress.stackexchange.com/questions/281550/…
    – BlueSuiter
    Oct 10, 2017 at 4:58
  • overall it looks same, but its looks like the answer from there is to show posts in every category (including main category or sub category). His first code actually works fine but he found a problem that posts with same category show twice (duplicate) which is he want to only show once and not duplicate. For my problem is I want to show posts with same tags (For example: tutorial) and seperate in category. If category 1 and 2 have a posts with tags tutorial it would be shown the post but seperated in their own category
    – GaroPpo
    Oct 10, 2017 at 5:10
  • By making minor tweaks in the code you can use this for your purpose. As, much as I know you have to change the taxonomy only. Rest is up-to you.
    – BlueSuiter
    Oct 10, 2017 at 5:14
  • Yup. I think that if I there's 1 more foreach for tags before posts it would be work. But I dont know how to add the query. Honestly its my first time playing with query on wordpress. So I dont know what to do.
    – GaroPpo
    Oct 10, 2017 at 5:16
  • What I want is category 1 show all post but only a post with tutorial tag. It would be work in category 2 and 3 or more category if the post have tutorial tag.
    – GaroPpo
    Oct 10, 2017 at 5:18

3 Answers 3

2

This will help to get the category post on tag archive page. Let me know.

<?php       

        $current_tag = single_tag_title( $prefix = '', $display = false );
        $categories = get_categories( array('hide_empty' => TRUE) );
        foreach($categories as $category) { ?>
        <?php
            $args=array(
                'posts_per_page' => -1, 
                'tag' => $current_tag,
                'cat' => $category->term_id,
            );

            $the_query = new WP_Query( $args );

            if ( $the_query->have_posts() ) {
                ?>
                <div class="CategoryPost">
                    <h2><?php echo "Tag: " . $current_tag; ?></h2>
              <h2><?php echo "Category: " . $category->name; ?></h2>
              <ul>
              <?php
                echo '<ul>';
                while ( $the_query->have_posts() ) {
                    $the_query->the_post();
                    echo '<li>Post title: ' . get_the_title() . '</li>';
                }
                echo '</ul></ul></div><br>';
            } 
            ?>

        <?php } 
        wp_reset_postdata();
7
  • Perfect Sidd! Finally what i looking for. Here the screenshots: prntscr.com/gvhz7m. Really really thank you so much!
    – GaroPpo
    Oct 10, 2017 at 7:20
  • cool. Glad i could help. It would be great if you could accept my answer :)
    – Sid
    Oct 10, 2017 at 7:22
  • Im the one who should be thanks! I just PM you on facebook. I Hope we can get in touch more through facebook. Thanks!
    – GaroPpo
    Oct 10, 2017 at 7:29
  • we sure will. let me know if u need any help in future.
    – Sid
    Oct 10, 2017 at 7:30
  • working for me after change attray value "$args=array( 'cat' => $category->term_id, 'post_type' => 'ourservices', //'posts_per_page' => 10, // 'category_name' => 'our-services', // replace it with your category slug 'orderby' => 'name', 'order' => 'ASC' );"
    – dev
    Oct 22, 2021 at 19:51
0

Please try this one and let me know if it helps.

<?php
        $categories = get_categories( array('hide_empty' => TRUE) );
    foreach($categories as $category) { ?>
    <?php
        $args=array(
            'posts_per_page' => -1, 
            'tag' => 'tutorial',
            'cat' => $category->term_id,
        );

        $the_query = new WP_Query( $args );

        if ( $the_query->have_posts() ) {
            ?>
            <div class="CategoryPost">
          <h2><?php echo $category->name; ?></h2>
          <ul>
          <?php
            echo '<ul>';
            while ( $the_query->have_posts() ) {
                $the_query->the_post();
                echo '<li>' . get_the_title() . '</li>';
            }
            echo '</ul></ul></div>';
        } 
        ?>

    <?php }
    wp_reset_postdata(); ?>

P.S. I have just printed the post title. If this works for you, please add other parameters to print as well.

Thanks

EDIT: to display for all tags.

   <?php 
        $tags_array = get_tags();
        $categories = get_categories( array('hide_empty' => TRUE) );
        foreach($tags_array as $tag){
        foreach($categories as $category) { ?>
        <?php
            $args=array(
                'posts_per_page' => -1, 
                'tag_id' => $tag->term_id,
                'cat' => $category->term_id,
            );

            $the_query = new WP_Query( $args );

            if ( $the_query->have_posts() ) {
                ?>
                <div class="CategoryPost">
                    <h2><?php echo "Tag: " . $tag->name; ?></h2>
              <h2><?php echo "Category: " . $category->name; ?></h2>
              <ul>
              <?php
                echo '<ul>';
                while ( $the_query->have_posts() ) {
                    $the_query->the_post();
                    echo '<li>Post title: ' . get_the_title() . '</li>';
                }
                echo '</ul></ul></div><br>';
            } 
            ?>

        <?php } }
        wp_reset_postdata(); ?>
15
  • Thanks! The code is working exactly like i want but If theres a category without the post. The Category name still showing. Here the screenshots: prntscr.com/gvh99f. But is it a option that the code would working on all tags? I mean the 'tag' => 'tutorial', can be change to make it work on all tags?
    – GaroPpo
    Oct 10, 2017 at 5:58
  • change the first line to $categories = get_categories( 'hide_empty' => true ); As you looped through all categories, you can loop through all tags using get_tags() function. Hope this helps.
    – Sid
    Oct 10, 2017 at 6:04
  • I've got Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW)
    – GaroPpo
    Oct 10, 2017 at 6:06
  • extremely sorry! that was my bad. $categories = get_categories( array( 'hide_empty' => true) );
    – Sid
    Oct 10, 2017 at 6:10
  • nope. dont feel sorry. you help me a lot. Thank you very much. But it still not working. Still have same problem. The other category name shown up. Am i skip something?
    – GaroPpo
    Oct 10, 2017 at 6:13
0

@GaroPpo Here, is tweaked code which I suggested to you. I hope this will full-fill you requirement.

<?php
    $tag_id = array(12, 13, 14);
    $taxonomy = 'testimonial-category';
    $postType = 'testimonial';
    $terms = get_terms(['taxonomy' => $taxonomy, 'orderby' => 'term_id', 'hide_empty' => true]);
?> <div class="add-accordion"> <?php
    foreach($terms as $term){
        if($term->parent == 0){
            echo '<h3>' . $term->name . '</h3>';
            continue;
        }  $posts = get_posts(array('post_status' =>'publish','post_type' => $postType, 'tag__in' => $tag_id,
                array(
                    'taxonomy' => $taxonomy,
                    'field' => 'term_id',
                    'terms' => $term->term_id,
                ),));
        ?>
        <div class="add-accordion">
            <h3><?php echo $term->name ?></h3>
            <div class="add-accordion">
                <?php foreach($posts as $post){ ?>
                    <h3><?php echo $post->post_title ?></h3>
                    <div class="">
                        <?php echo get_the_content($post->ID) ?>
                    </div>
                <?php } ?>
            </div>
        </div>
        <?php
    }
?>
2
  • Thanks! I try to understand the code. Hopefully i can try this code.. Appreciate your help a lot!
    – GaroPpo
    Oct 10, 2017 at 7:07
  • Thanks! I try to understand the code. Hopefully i can try this code.. Appreciate your help a lot!
    – GaroPpo
    Oct 10, 2017 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.