Display single post for each custom post type tag

20 Views Asked by At

I'm working on a new website for a church. I have created a custom post type for their sermons. Some of the sermons are a part of a series, so I have set it up for the series name to be a tag.

I'm trying to create a query to display a list the latest or a single post for each tag. I found the following article as it seems similar to what I am needing, but am unable to get it to work:

https://wordpress.stackexchange.com/questions/290620/how-to-show-only-one-post-for-each-categories-of-taxonomy-of-custom-post-that-co

Here is the code I tried just for testing:

<?php
$tags = get_terms( 
array(
    'taxonomy' => 'prayer',
)
);

foreach ( $tags as $tag ) {

$args = array(
    'post_type'       => 'sermon',
    'posts_per_page'  => '1',
    'tax_query'        => array(
        array(
            'taxonomy' => 'prayer',
            'terms'    => $tag->term_id,
        ),
    )
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        
        echo '<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>';
    }
}
}
?>

This doesn't display anything even though there are 2 posts with the tag "prayer". I only place "prayer" in the array for testing purposes. I don't want specify the taxonomy in the code as I want it to look at all of the tags.

0

There are 0 best solutions below