0

I have added a custom taxonomy as "Tempo" to denote whether the songs people are searching for are Up Tempo, Mid Tempo or Slow Tempo. The search filters them properly, but I am unable to get the taxonomy to display on the search results page properly without a Warning popping up. This is the code I am using so far on the search.php template-part (pls keep in mind I am not great at PHP and still learning) which is a mix of the theme developers search.php code and some other snippets I found while digging around to accomplish this task.

<div class="search-page-content">
        <?php if ( have_posts() ) : ?>
            <?php
            while ( have_posts() ) :
// Get the taxonomy terms
        $_terms = get_terms( array('tempo') );
        foreach ($_terms as $term) :
        $term_slug = $term->slug;
        $term_link = get_term_link( $term );
        $_posts = new WP_Query( array(
          'tax_query' => array(
            array(
              'taxonomy' => 'tempo',
              'field'    => 'slug',
              'terms'    => $term_slug,
                ),
            ),
        ));
// Show the post results
                the_post();
                printf( '<h2><a="%s">%s</a></h2>', esc_url( get_permalink() ), esc_html( get_the_title() ) );
                echo '<div class="search-meta-section">';
                
                echo '<div class="search-tempo">';
                echo '<a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></ul>';
                echo '</div>';
                
                echo '<div class="search-category">'; the_category(); echo '</div>';
                echo '</div>';

                the_content();
                wp_reset_postdata();
                endforeach;
                endwhile;
            ?>

        <?php else : ?>

            <p><?php esc_html_e( 'It seems we can\'t find what you\'re looking for.', 'hello-elementor' ); ?></p>
        <?php endif; ?>
    </div>

The Warning I am getting on the page is:

Warning: Undefined array key 5 in /homepages/31/d589186062/htdocs/coveracapellas/wp-includes/class-wp-query.php on line 3571

To be clear, the taxonomy terms ARE showing where I want them, and are clickable links to browse more items tagged with that taxonomy, but the Warning is what is throwing me for a loop.

1 Answer 1

1

I'm having some trouble understanding your code. It looks like you're:

  1. Looping over every search result.
  2. For each result, getting all tempo terms.
  3. For each tempo, querying all posts that belong to that term.
  4. Doing nothing with the queried posts.

This is all extremely inefficient and doesn't do anything like what you say you're trying to do.

If you want to display the list of tempos assigned to a search result in search.php you just need to use the_terms(). The result should look like this:

<div class="search-page-content">
    <?php if ( have_posts() ) : ?>
        <?php while ( have_posts() ) : the_post(); ?>
            <h2>
                <a href="<?php the_permalink(); ?>">
                    <?php the_title(); ?>
                </a>
            </h2>
            <div class="search-meta-section">
                <div class="search-tempo">
                    <?php the_terms( get_the_ID(), 'tempo' ); ?>
                </div>
                <div class="search-category">
                    <?php the_category(); ?>
                </div>
            </div>
            <?php the_content(); ?>
        <?php endwhile; ?>
    <?php else : ?>
        <p>
            <?php esc_html_e( 'It seems we can\'t find what you\'re looking for.', 'hello-elementor' ); ?>
        </p>
    <?php endif; ?>
</div>
1
  • Thank you so much for this. It seems to be working correctly, and I am still learning about php so thank you! I didn't find the_terms() anywhere in my searching for help on this, in relation to custom taxonomies. I thought they were for tags or categories! Feb 9, 2023 at 20:16

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.