0

I'm having trouble getting this to work. I've created a custom taxonomy named article_subjects and am looping thru, displaying each term; but am trying to get the newest post for each term

    <section>
    <?php
        $args = array (
            'orderby'    => 'name',
            'order'      => 'ASC',
            'hide_empty' => true,
            'number' =>      '4'
        );

        $terms = get_terms( 'article_subjects', $args );
        foreach ( $terms as $term ) { ?>
            <div>

            <?php 
                $postargs = array (
                    'tax_query' => array (
                        array (
                            'taxonomy' => 'article_subjects',
                            'field'    => 'name',
                            'terms'    => $term->slug,
                            'number'    => '1'
                        )
                    )
                );
                global $wp_query;
                $posts = new WP_Query($args);

                foreach ( $posts as $post ) { ?>
                    <p class="date"><?php echo $post->the_date ?></p>
                    <h3><?php echo $post->the_title ?></h3>
                    <p class="description"><?php echo $post->the_excerpt ?></p>
                <?php } ?>
            </div>
        <?php }
    ?>
</section>
  • what's not working with your code presently? note that number isn't a valid argument in tax_query, I assume you want to set posts_per_page. – Milo Jun 15 '14 at 0:00
  • No need to call the global wp_query – Pieter Goosen Jun 15 '14 at 6:22
  • You should definitely consult the Codex on WP_Query instead of guessing how it works. By the way: $post->the_date, $post->the_title and $post->the_excerpt are not valid properties. – birgire Jun 15 '14 at 9:39
  • No guessing, just having trouble with it. Got it working now though. – davidcondrey Jun 15 '14 at 10:10
0

There are a few things wrong:

  1. In your code you are using the same $args array in the WP_Query call that you used in your get_terms call. Perhaps you meant to use $postargs in the WP_Query call?

  2. @Milo is correct...you should be using posts_per_page, not number.

  3. Your tax_query array element 'field' says 'name' but you pass it the term slug and not the term name.

Hopefully this helps: Here is the reference

0

Getting things mixed up before, got it working now. Thanks. This is the code I finally ended up with for my purposes:

<section id="top-columns">
    <?php $args = array (
        'orderby'    => 'name',
        'order'      => 'ASC',
        'hide_empty' => true,
        'number' =>      '4'
    );

    $terms = get_terms( 'article_subjects', $args );
    foreach ( $terms as $term ) {
        $post_args = array (
            'post_type'  => 'post',
            'article_subjects' => $term->name,
            'posts_per_pge'    => '1'
        );

        $query = new WP_Query( $post_args );

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

            <div id="post-<?php the_ID(); ?>">
                <?php 
                    the_date();
                    the_title();
                    the_excerpt();
                ?>
            </div>
        <?php }

        wp_reset_postdata();
       } ?>
</section>
  • Glad I could help. I'm sure you caught it but there's a typo in 'posts_per_pge (page)' – leromt Jun 15 '14 at 13:28

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.