0

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.

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

if ( have_posts() )
the_post();
?>

<?php
// 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; ?>
</h2>

<?php
    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'>

<?php
// 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 } ?>

Thanks

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

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

| improve this answer | |

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.