WordPress.org

Ready to get started?Download WordPress

Forums

Archive: Custom post type and taxonomy (2 posts)

  1. DennisMadsen
    Member
    Posted 4 years ago #

    I've created a custom post type and a custom taxonomy, both called reference. The custom taxonomy is hierachical and I've created values (categories) like web, print etc. For each of the reference custom post type, I choose one of these custom taxonomy values.

    These reference custom post types should be displayed on a archive on the URL references/. I've created a custom template (template-reference.php) and a page called reference using this template. I don't know if this is the best way for creating a archive - please advice me, if I could do it different.

    On the archive, the reference custom post types should be ordered first by the taxonomy and next the menu order. First showing all web references and next all print references.

    In the template-reference.php file, my query looks like this:
    $my_query = new WP_Query('post_type=reference&orderby=menu_order&order=ASC');

    This will only sort the references by the menu_order - but I would like to group them in the different taxonomy, as explained before. How can I do that?

    I'm wondering, if I first, in some way, could get a list of all the created taxonomies and then do the query for each taxonomies. But I don't know how to do that.

    I hope someone could help me. Please tell me, if you don't understand the situation.

  2. MichaelH
    Member
    Posted 4 years ago #

    Have used this lately, but might help you:

    <?php
    //get desired post_type(s)
    //get all taxonomies for each post_type
    //for each taxonomy get all terms
    //for each term display all posts in that post_type/taxonomy/term
    
    $args=array(
      'name' => 'book',
      'publicly_queryable' => true,
      'exclude_from_search' => false
    );
    $output = 'objects';
    $post_types=get_post_types($args,$output);
    
    foreach ($post_types  as $post_type ) {
      $object_type = $post_type->name;
      $taxonomies = get_object_taxonomies($object_type);
    
      foreach ($taxonomies  as $taxonomy ) {
        $args=array(
          'name' => $taxonomy
        );
        $output = 'objects'; // or objects
        $tax=get_taxonomies($args,$output);
        $tax_terms = get_terms($taxonomy);
    
        foreach ($tax_terms  as $tax_term) {
          $args=array(
            'post_type' => $object_type,
            "$taxonomy" => $tax_term->slug,
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'caller_get_posts'=> 1
          );
          $my_query = null;
          $my_query = new WP_Query($args); 
    
          if( $my_query->have_posts() ) {
            echo '<p>Listing post_type='.$post_type->label . ', taxonomy='. $taxonomy . ', term='. $tax_term->slug.'</p>';
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
              <?php
              echo get_the_term_list( $my_query->post->ID, $taxonomy, $tax[$taxonomy]->singular_label.': ', ', ', '' );
            endwhile;
          }
        }
      }
    }
    wp_reset_query();
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags