I have two custom post types, “sermons” and “series”. Sermons has custom fields with it, one of which is a relationship field back to “series”. When you view an individual sermon, I’d like to populate a query that shows other sermons from the same series, but can’t seem to figure out how to do it! Could anyone at least point me in the right direction? If the info is out there, I can’t find the right Google search terms to find it. Thanks!
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!
This is very useful information!
I would like to know why arrays as value don’t seem to work for this either using “LIKE” or “IN”?
I have been banging my head against the wall for hours with this…
If I put a single ID with the compare “LIKE” it works like a charm. But if I put an array in the value, it queries no posts whatsoever…
@jpveix the reason that it does not work is that the field is stored as a serialized array. This is an array turned into a string.
Thanks for the answer!
It turns out I also found out about this very shortly before you answered…
I had searched for ways to use it and none of the example made mention of that. Maybe it is so evident that only a dumb dumb like me would not know it 😉