Support

Account

Home Forums General Issues Query for other posts with same relationship Reply To: Query for other posts with same relationship

  • I finally accomplished this by doing the following:

    <?php
    if( $posts ):
        foreach( $posts as $post): // variable must be called $post (IMPORTANT)
            setup_postdata($post);
            $seriesID = get_the_ID();
        endforeach;
        wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
    endif; 
    $sermons = new WP_Query(array(
        'post_type' => 'sermon',
        'meta_query' => array(
            array(
                'key'     => 'series',
                'value'   => $seriesID,
                'compare' => 'LIKE'
            )
        ),
        'order'	=> 'DESC'
    ));
    ?>

    I’d love to know if there’s a better way, though!