I'm trying to make some archive list where the posts (some custom type posts) are grouped by a tag, or maybe a custom field, I was trying to replicate How to group by taxonomy on Custom Post Type archive , althought I get the list I cannot get the tag Title that the posts are grouped by, I'm a little lost here, thought that maybe I need some double foreach or something like that.

What I want is this:

Country (tag)
--Area 1 (post)
--Area 2 (post)

Country 2 (tag)
--Area 3
--Area 4

The code:

Edit: this code now works fine, it's just the code from the linked content but some syntax errors.

$taxonomy = array( "name" => 'zonas' , "slug" => 'zonas'); //zonas is the taxonomy
$custom_post_type = "material";

if ( have_posts() )

// Query your specified taxonomy to get, in order, each category
$categories = get_terms($taxonomy['name'], 'orderby=title');
foreach( $categories as $category ) {

<div id="content">    
<h2 class="page-title">
<?php echo $category->name; ?>

    global $post; // Access the global $post object.

    // Setup query to return each custom post within this taxonomy category
    $o_queried_posts = get_posts(array(
        'nopaging' => true,
        'post_type' => $custom_post_type,
        'taxonomy' => $category->taxonomy,
        'term' => $category->slug,

<div id='archive-content'>

// Loop through each custom post type
foreach($o_queried_posts as $post) : 
    setup_postdata($post); // setup post data to use the Post template tags. ?>

    <div id="post-<?php the_ID(); ?>">

       <h2 class="entry-title"><?php the_title(); ?></h2>

    </div><!-- #post -->
<?php endforeach; wp_reset_postdata(); ?>

</div> <!-- archive-content -->

</div> <!-- #content -->
<?php } ?>


  • Could you show us your current code, pls? Commented Mar 27, 2019 at 19:47
  • Sure, I've added to the question, sorry if it makes no sense, I'm a little lost.
    – jarayama
    Commented Mar 27, 2019 at 20:07

1 Answer 1


There was some syntax errors, but fixing and combining the answers from the referenced post makes that code that works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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