0

This function seems like it should work, and it mostly does, but what I'm lacking are the values for the 'author' key that ends the list item, prefaced by the text 'By'.

What I'd like is for the function to loop through the taxonomy and return all the custom posts that are ascribed to it, and for each post I'd like to see that custom field's value. Am I using the wrong WP function to call the custom field?

/**
* List custom post type posts of custom taxonomy
*/
function get_all_of_a_taxonomy(){
    $post_type = 'entry';
    $tax = 'issue';
    $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
        );

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

            if( $my_query->have_posts() ) : ?>

                <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

                            <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a> by <?php echo get_post_meta($post->ID, 'author', true); ?></li>

                <?php endwhile;?>
            <?php else : ?>
            <?php endif; // if have_posts()
            wp_reset_query();

        }
    }
}

2 Answers 2

1

Your query should work if you call global $post and don't reset the query (not needed when using WP_Query).

You can also try using tax_query.

$tax = 'issue';
$terms = get_terms( $tax );

   foreach ( $terms as $term ) :

echo $term->name;

 $args = array(
    'post_type' => $post_type,
    'post_per_page' => -1,
    'post_status'   => 'publish',
    'caller_get_posts' => 1
    'tax_query' => array(
             array(
               'taxonomy' => $tax,
               'field'    => 'ID',
               'terms'   => array( $term->term_id )
             )
           )
     );
 $my_query = new WP_Query($args); ?>

      <ul class="<?php echo $term->slug; ?>">
          <?php  if( $my_query->have_posts() ) : ?>

                <?php while ( $my_query->have_posts() ) : $my_query->the_post(); global $post; ?>

                            <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a> by <?php echo get_post_meta($post->ID, 'author', true); ?></li>

                <?php endwhile; endif; ?>

    </ul>

<?php endforeach; ?>
5
  • Brilliant! That worked perfectly. I knew there was something simple I'd overlooked.
    – jcnv
    Nov 7, 2011 at 14:35
  • Any reason, though, that this modification now queries all custom posts of the entry type display, rather than only those associated with the issue taxonomy?
    – jcnv
    Nov 7, 2011 at 16:16
  • Try leaving out the post_type
    – Chris_O
    Nov 7, 2011 at 16:56
  • That makes sense, and I bet it'd make more sense if I took your advice to use tax_query, since I see my query usage is now deprecated. But I couldn't get tax_query to work, so I stuck with my original function, plus global $post. That led me http://pastebay.com/143910.
    – jcnv
    Nov 7, 2011 at 17:44
  • Alright. Worked my way through this one. Thanks again for your help, Chris. If anyone is curious, here is the solution: pastebay.com/143959.
    – jcnv
    Nov 8, 2011 at 2:03
0

Well make it easy for yourself, use the wordpress function

http://codex.wordpress.org/Function_Reference/the_author

1
  • I may do that eventually, but I'm trying to push something live rather quickly, so I went with the custom fields for now. It seems like the_author involves a bit more setup in terms of authoring posts.
    – jcnv
    Nov 7, 2011 at 14:35

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.