2

I have registered a custom post type "Animals" and also two custom taxonomies ("Vertebrate" and "Type") assigned to it, which have their own specific terms, for "Vertebrate" it will be "Mammals" and "Reptiles", and for "Type" it will be "water type" and "ground type".

By using page.php I would like to display a list of all custom posts (Animals) that are sorted by terms that are assigned to the “Vertebrate” custom taxonomy, in this I succeed by using the below code:

<?php
        $post_type = 'animals';
        $tax = 'vertebrate';
        $tax_terms = get_terms($tax);

        if ($tax_terms) {
          foreach ($tax_terms  as $tax_term) {
            $args=array(
              'post_type'       => $post_type,
              "$tax"            => $tax_term->slug,
              'post_status'     => 'publish',
              'posts_per_page'  => -1,
              'caller_get_posts'=> 1,
              'orderby'         => 'title',
              'order'           => 'ASC'                    
            );

            $my_query = null;
            $my_query = new WP_Query($args);

            if( $my_query->have_posts() ) {

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

              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

              endwhile;
            }
            wp_reset_query();
          }
        }
?>

This gives me something like this:

Mammals:

  • Cow
  • Dog
  • Dolphin
  • Orca
  • Whale

Reptiles:

  • Lizard
  • Sea Turtle

However I would also like to display in the foregoing list to which terms of the "Type" taxonomy each post is assigned to (besides terms from the Vertebrate taxonomy) by simply returning their name after the post link.

I'm trying to achieve something like this:

Mammals:

  • Cow (ground type)
  • Dog (ground type)
  • Dolphin (water type)
  • Orca (water type)
  • Whale (water type)

Reptiles:

  • Lizard (ground type)
  • Sea Turtle (water type)

I’m completely stuck with this, also my knowledge about WordPress and PHP is virtually null, so I would gladly accept any help in this matter.

UPDATE


My friend helped me out with this. It should work with this:

            <?php

        $post_type = 'animals';
        $tax = 'vertebrate';
        $tax_terms = get_terms($tax);
        if ($tax_terms) {
          foreach ($tax_terms  as $tax_term) {
            $args=array(
              'post_type' => $post_type,
              "$tax" => $tax_term->slug,
              'post_status' => 'publish',
              'posts_per_page' => -1,
              'caller_get_posts'=> 1,
              'orderby'        => 'title',
              'order'          => 'ASC'                 
            );

            $my_query = null;
            $my_query = new WP_Query($args);
            if( $my_query->have_posts() ) {

              echo '<h2>'. $tax_term->name .'</h2>';
              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>
                <?php 
                setup_postdata( $post );
                $terms = wp_get_post_terms( $post->ID, 'type');
                echo '<span>'. ($terms[0]->name) .'</span></p>'; ?>

                <?php
              endwhile;
            }
            wp_reset_query();
          }
        }
        ?>
1

Something like this should help:

<?php
    $post_type = 'animals';
    $tax = 'vertebrate';

    $tax_terms = get_terms($tax);

    if ( $tax_terms ) {
        foreach ($tax_terms as $tax_term) {
            $args = array(
                'post_type' => $post_type,
                "$tax" => $tax_term->slug,
                'post_status' => 'publish',
                'posts_per_page' => -1,
                'caller_get_posts' => 1,
                'orderby' => 'title',
                'order' => 'ASC'                 
            );

            // $my_query = null;  <- REMOVE THIS, YOU DON'T NEED TO NULL VARIABLE BEFORE ASSIGNING IT'S VALUE

            $my_query = new WP_Query($args);
        ?>

            <?php if ( $my_query->have_posts() ) : ?>
                <h2><?php echo esc_html($tax_term->name); ?></h2>'; <?php // <- YOU SHOULD ESCAPE PRINTED VALUES ?>
                <ol>
                <?php while ($my_query->have_posts()) : ?>
                    $my_query->the_post(); ?>
                    <li>
                        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                        <?php 
                            // setup_postdata( $post ); <- THERE IS NO NEED TO SETUP_POSTDATA HERE (YOU'VE ALREADY CALLED the_post() ON QUERY
                            $type_terms = wp_get_post_terms( $post->ID, 'type');
                            if ( $type_terms ) {
                                echo '<span>('. esc_html($terms[0]->name) .')</span>';
                            }
                        ?>
                    </li>
                <?php endwhile; ?>
                </ol>
            <?php endif; ?>
        <?php
        // wp_reset_query(); <- YOU DON'T CHANGE GLOBAL $wp_query, SO THERE IS NO NEED TO RESET QUERY (ESPECIALLY DON'T DO THIS FOR EACH $tax_term
        }
    }
    // BUT YOU DO CHANGE GLOBAL $post, SO YOU SHOULD RESET IT'S VALUE TO BE COMPATIBLE WITH PLUGINS AND SO ON
    wp_reset_postdata();
?>

Ordered lists (OLs) would be much better than paragraphs (P) in this case (since these are LISTS of animals ORDERED BY name).

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.