How can I build an archive page for a custom post type and 2 taxonomies?

Hello,

First I give you the elements that I have:
3 custom post types:
— community — artist — museum

4 custom taxonomies:
— countries, with the terms: denmark; sweden; finland etc.
— communities, with the terms: art festival; art fairs etc.
— artists, with the terms: painters, photographers, sculptors etc.
— museums, with the terms: museums, galleries, alternative spaces etc.

An example of what I would like to achieve with the archive is this:
having posts for the Community (custom post type) from the Countries (custom tax) for a specific term (Denmark).
Those posts should be listed by communities (custom tax) terms.

Then one should be able to switch to the Artist, or Museum (custom post type) for the same country and fetch the appropriate posts for each terms.
Of course I need to be able to do the same for each country.

I tried to create templates for each country like so:
taxonomy-countries-denmark.php
and built a loop with the following code:

<?php
//start by fetching the terms for the communities taxonomy
$terms = get_terms( 'communities', array(
    'orderby'    => 'name',
    'order'      => 'ASC',
    'hide_empty' => 0
) );
?>
<?php
// run a query for each community
foreach( $terms as $term ) {

    // Define the query
    $args = array(
        'post_type' => 'community',
        'countries' => 'denmark',
        'communities' => $term->slug
    );
    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {

echo'<h2>' . $term->name . '</h2>';

   	 while ( $query->have_posts() ) : $query->the_post(); ?>

    <article <?php post_class('col-sm-6 col-lg-4'); ?>>
<div class="article-container">
<header>
<h2 class="entry-title"><?php the_title(); ?></h2>
</header>
<div class="">
    <?php the_content(); ?>
</div>
</div>
</article>

<?php endwhile;

// restore orginal query
   	 wp_reset_postdata();
   	}
} ?>

So far, this loop displays the list of posts for the communities terms
for the defined country just like I need, but the url for this page is:
site-example.com/countries/denmark/

How can I query another custom post type for the same country with such a url?
Is it possible to have the url write something like this:
site-example.com/countries/denmark/communities

Maybe with a mysterious plugin that I didn’t find yet?

If it’s possible, can you please explain how I could do that?

Then finally if it’s possible to have a url that have 2 different custom taxonomies,
I imagine I could create a loop with if — elseif statements to decide which taxonomies terms I want to have
and call the proper url to do it.

Unfortunately I can’t give you a link to this as I’m working on it locally,
but the live site has a version that shows what I’m trying to do:
puf-art.com/art-guide/communities/denmark/
In this version of the site, the all thing is built using pages.
The entire content has to be typed manually for each page.
This is why we are updating that part of the site, so it can be updated with posts.

I hope this was clear enough
Let me know if you need more information.

Best,
Sébastien

  • Vaughan
    • Support/SLS MockingJay

    Hi @mmarauder,

    This is a bit beyond my expertise.

    However, you might be able to do this using permalink rewriting.

    see http://wordpress.stackexchange.com/questions/55351/permalink-rewrite-with-custom-post-type-and-custom-taxonomy/57586#57586

    http://wordpress.stackexchange.com/questions/57493/custom-taxonomy-specific-to-a-custom-post-type

    http://codex.wordpress.org/Template_Hierarchy

    http://code.tutsplus.com/tutorials/taxonomy-archives-list-posts-by-taxonomys-terms--cms-20045

    Hope this helps

  • mmarauder
    • Design Lord, Child of Thor

    Hello,

    I'm coming back to this post because I'm still struggling with this. I changed a little bit the implementation, so here is a description of what I'm trying to do now.

    So I still have the same elements.
    Custom post types and taxonomies created with the CPT UI plugin.

    The 3 custom post types are:
    artist, community, museum

    The 3 custom taxonomies are :
    Communities (attached to the Community post type),
    Museums (attached to the Museum post type),
    Countries (attached to the Artist, Community and Museum post types)

    The communities taxonomy has several terms:
    ex: Art Associations, Art Fairs, Art Festivals…
    The museums taxonomy has the following terms: Alternative Spaces, Galleries, Museums.
    The countries taxonomy contains country names of course.

    I also created a countries navigation menu that I activated in my theme and set it to display for the countries taxonomy.

    I set custom fields to input the information for each custom post using the Advanced Custom Fields plugin.

    Now, I'm going to describe as clearly as I can what I'm trying to do.

    For each country I want to display the following (in that order):
    1. The Artists profiles.
    2. Then, the Communities profiles separated by terms in ascending order.
    3. Then the Museums profiles, also separated by terms in the same fashion.

    Then there is a side menu to allow visitors to jump from one section of the page to another like on the twitter bootstrap site.

    What I've done so far.

    I've been trying to get this to work for about 3 weeks now but I'm no php master. And even though I tried, I don't think I'm going to find the answer on my own.

    First, I created a taxonomy.php file where I tried the regular WordPress loop. This is displaying all the posts for a specific country like I want, but I can't find a way to separate each section/post type and display the terms for the custom taxonomies. Code below.

    First the code to call the menu on index.php

    <?php if (is_tax('countries')) : ?>
        <nav class="categories-nav tk-proxima-nova-extra-condensed" role="navigation">
            <h1>Countries:</h1>
            <?php
                if (has_nav_menu('countries_navigation')) :
                    wp_nav_menu(array('theme_location' => 'countries_navigation', 'menu_class' => 'nav category-selector'));
                endif;
            ?>
        </nav>
    <?php endif ?>

    Here the code for the regular WordPress loop that I tried in taxonomy.php

    <?php if ( have_posts() ) : ?>
        <?php while (have_posts()) : the_post(); ?>
            <article <?php post_class('col-sm-6 col-lg-4'); ?>>
                <div>
                    My content using custom fields.
                </div>
            </article>
        <?php endwhile; ?>
    <?php endif; ?>

    Then I tried the same code I sent you in my first question that only displays content for one country, see code below:

    <?php
    $terms = get_terms( 'communities', array(
        'orderby'    => 'name',
        'order'      => 'ASC',
        'hide_empty' => 0
    ) );
    ?>
    <?php
    foreach( $terms as $term ) {
    
        $args = array(
            'post_type' => 'community',
            'countries' => 'denmark',
            'communities' => $term->slug
        );
        $query = new WP_Query( $args );
    
        if ( $query->have_posts() ) {
    
            echo'<h2>' . $term->name . '</h2>';
    
            while ( $query->have_posts() ) : $query->the_post(); ?>
    
            <article <?php post_class('col-sm-6 col-lg-4'); ?>>
                <div>
                    My content using custom fields.
                </div>
            </article>
    
            <?php endwhile;
    
            wp_reset_postdata();
        }
    } ?>

    The last code example is displaying only the terms for Communities and for one country only. From there I tried all kinds of things to try to get it to display what I want, but every time I change the loop, it stops working and doesn't display a single thing.

    Is there someone out there who can help me?

  • Vinod Dalvi
    • WP Unicorn

    Hi Sébastien,

    Thank you for your reply.

    You can achieve all these things by developing multiple WordPress loops using WP_Query class and its arguments in the appropriate WordPress template files.

    You will find more information about it on the following pages.

    http://codex.wordpress.org/Class_Reference/WP_Query
    http://codex.wordpress.org/Template_Hierarchy

    Developing custom code for custom functionality is beyond the scope of support that we provide. If you are not a programmer then you can consider hiring a developer from the following page to develop it for you.

    https://premium.wpmudev.org/wordpress-development/

    Any other issues, we're here to help.

    Cheers,
    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.