bezierer
bezierer

Reputation: 388

Error trying to get the taxonomy term name

Hello—so what I'm attempting is to get 2 posts from 2 taxonomy terms, I've got that part done, where I'm borking up is trying to display the term name above the posts. I've done this before using a foreach loop but no matter what I change, I keep getting the error that I've supplied invalid arguments for the loop. After a lot of googling, I'm a bit lost and wondering if you folks have some guidance for me?

$current_post_id = get_posts( array(
        'post_type'      => 'issue',
        'posts_per_page' => 1,
        'fields'         => 'ids',
    ) );

    $terms = get_the_terms( $current_post_id, 'department' );

        foreach ( $terms as $term ) {

            $args = array(
                'connected_type'      => 'posts_to_issues',
                'connected_direction' => 'to',
                'connected_items'     => $current_post_id,
                'post_type'           => 'post',
                'posts_per_page'      => 2,
                'tax_query'           => array(
                    array(
                        'taxonomy' => 'department',
                        'field'    => 'slug',
                        'terms'    => array( 'washington-watch', 'equipment-spotlight'),
                    )
                ),
            );

            $sidebar_query = new WP_Query( $args ); 

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


           <?php echo '<h2>' . $term->name . '</h2>' ?>

            <figure>
                <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail( 'sidebar-thumb-med' ); ?>
                </a>
             </figure>

            <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>

        <?php the_excerpt();

        endwhile;

        }

        // Restore global post data
        wp_reset_query();  

Upvotes: 0

Views: 227

Answers (1)

rnevius
rnevius

Reputation: 27112

$current_post_id is an array with one item (because passing in a return fields argument returns an array). You need to refer to that one item in the following cases:

$terms = get_the_terms( $current_post_id[0], 'department' );

...and...

'connected_items' => $current_post_id[0],

Upvotes: 1

Related Questions