1

I'm using this code on a search.php template to display search results, posts are formatted differently using a template part depending on the post term. This method works great on the index.php but I'm having trouble getting it to work on the search.php template. It works to display the posts but it displays a Invalid argument supplied for foreach() warning for the line foreach( $terms as $term ) {

Any ideas? Thanks.

        <div class="container">
            <div class="gutter-sizer"></div>

            <?php

            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            global $query_string;

            $query_args = explode("&", $query_string);
            $search_query = array();

            foreach($query_args as $key => $string) {
                $query_split = explode("=", $string);
                $search_query[$query_split[0]] = urldecode($query_split[1]);
            }

            $search_query['post_type'] = 'interpreted'; // your custom post type
            $the_query = new WP_Query($search_query); ?>

            <?php if ( $the_query->have_posts() ) : ?>
                <div id="isotope-list">

                    <?php while ( $the_query->have_posts() ) : $the_query->the_post();
                    $termsArray = get_the_terms( $post->ID, "type" );  //Get the terms for this particular item
                    $termsString = ""; //initialize the string that will contain the terms
                    foreach ( $termsArray as $term ) { // for each term
                    $termsString .= $term->slug.' '; //create a string that has all the slugs
                }
                ?>

                <div class="<?php echo $termsString; ?> item interpreted"> <?php // 'item' is used as an identifier (see Setp 5, line 6) ?>

                    <?php

                    $terms = get_the_terms( $post->id, 'type', array( 'parent' => 0 ) );
                    $terms_slugs = array();
                    foreach( $terms as $term ) {
                        $terms_slugs[] = $term->slug;
                    }

                    if( !empty($terms_slugs) ) :
                        get_template_part( 'blocks/block', array_pop($terms_slugs) );
                    else : endif;
                    ?>

                </div> <!-- end item -->

            <?php endwhile;  ?>
        </div> <!-- end isotope-list -->

    </div> <!-- end container -->
    <?php cambridgerules_paging_nav(); ?>

<?php else : ?>

    <?php get_template_part( 'content', 'none' ); ?>

<?php endif; ?>
2

Because get_the_terms will not always return an array; it will be false if the post has no terms, or a WP_Error if the taxonomy does not exist - always sanity check the result before looping:

$terms_slugs = array();
$terms = get_the_terms( $post->id, 'type', /* Note: get_the_terms does not take a 3rd argument ==> */ array( 'parent' => 0 ) );

if ( is_array( $terms ) ) {
    foreach ( $terms as $term ) {
        $terms_slugs[] = $term->slug;
    }
}

Better yet, in this case you can use the awesome helper wp_list_pluck():

if ( is_array( $terms = get_the_terms( $post->ID, 'type' ) ) )
    $terms_slugs = wp_list_pluck( $terms, 'slug' );
else
    $terms_slugs = array();

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.