Show loop content according to value, inside another loop

Scenario:

I have a post type portfolio and a custom post type song, and also a custom taxonomy called songs_categories.

Read More

Portfolio is inside loop 1 and the songs are in loop 2, which is inside loop 1.

What I need to do is to show the songs in the portfolio but only if the songs category slug (album name) has the actual value of the portfolios post_name (album name).

I can get the values needed but I’m having trouble writing the actual function necessary to do the filtering. Any help is appreciated.

<!-- Loop 1 -->

<div class="songs">

<?php 

  $var1 = $post->post_name;  
  $my_query = new WP_Query('post_type=song' );

    while ( $my_query->have_posts() ) : $my_query->the_post();

?>

    <?php $terms = get_the_terms( $post->ID , 'songs_categories' ); 
        foreach ( $terms as $term ) {
            $term_link = get_term_link( $term, 'songs_categories' );
            if( is_wp_error( $term_link ) )
            continue;

        echo "$var1";
        echo "$term->slug";

       } ?>

    <div class="col one">

        <a href="<?php echo get_permalink(); ?>"><?php the_post_thumbnail(); ?></a>

         h2><?php the_title(); ?></h2>  

        <?php
        $autor_name = get_post_meta($post->ID, "_cmb_autor_text", false);
        if ($autor_name[0]=="") { ?>

        <!-- If there are no custom fields, show nothing -->

        <?php } else { ?>

        <?php foreach($autor_name as $autor_name) {
        echo '<p>'.$autor_name.'</p>';
        } ?>

    <?php } ?>  
    </div> 

<?php endwhile; ?>  <!-- End 2nd loop -->
</div> <!-- End songs -->

<?php endif; endwhile; ?>

<?php endif; // password check ?> <!-- End 1st loop -->

Related posts

Leave a Reply

2 comments

  1. Got the desired result by modifying my query:

    <?php 
    
    $var1 = $post->post_name;
    
    $args = array(
        'post_type' => 'song',
        'tax_query' => array(
            'relation' => 'OR',
            array(
                'taxonomy' => 'songs_categories',
                'field'    => 'slug',
                'terms'    => $var1,
            ),
    
        ),
    );
    
    $query = new WP_Query( $args );
    
    while ( $query->have_posts() ) : $query->the_post();
    
    ?>