0

Title says it all. I'm trying to link to the most recent post of a custom post type, but only within the same term. Currently my code successfully echos the correct term ID but doesn't show a link on the screen.

<?php

    // Get the ID of the current posts's term
    $terms = get_the_terms( get_the_ID(), 'comic-series' );

    // Only get the parent term and ignore child terms
    foreach ( $terms as $term ){
        if ( $term->parent == 0 ) {
            
            // Echo the term (this is only for debugging purposes)
            echo $term->term_id;
            
            // Take only the most recent post of same term
            $args = array( 'numberposts' => '1', 'category' => $term->term_id );
            $recent_posts = wp_get_recent_posts( $args );
            
            // Link to most recent post of same term
            foreach( $recent_posts as $recent ){ ?> 
                <a href="<?php get_the_permalink( $recent->ID ); ?>">
                    >>  
                </a> <?php
            }
        }
    } 
    
?>
1

I ended up using wp_query instead because I couldn't make wp_get_recent_posts work no matter what I tried. For anyone who needs this functionality in the future, here's the code I used:

// Get the ID of the current posts's term
    $terms = get_the_terms( $post->ID, 'comic-series' );

    // Only get the parent term and ignore child terms
    foreach ( $terms as $term ){
        if ( $term->parent == 0 ) {

            // Query Options
            $query_options = array(
                'posts_per_page' => 1,
                'orderby' => 'title', // I've ordered all my posts by title but change this for your needs 
                'order' => 'DESC',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'comic-series',
                        'field'    => 'term_id',
                        'terms'    => $term->term_id,
                    ),
                ),
             );
             
            //Query
            $the_query = new WP_Query( $query_options ); 

            while ($the_query -> have_posts()) : $the_query -> the_post();
             
                // Link to the latest page
                ?> <a href="<?php the_permalink(); ?>">LINK TEXT</a> <?php
             
            endwhile;
            wp_reset_postdata();    
            
        }
    } 
0

Not sure if this is the only error, but watch out for WP functions that have get_ on the front, as they return the value in PHP without echoing it to the screen. The ones without will echo it.

So probably you want:

            foreach( $recent_posts as $recent ){ ?> 
                <a href="<?php the_permalink( $recent->ID ); ?>">
                    >>  
                </a> <?php
            }

Note removal of get_ from the function

3
  • I think the issue might have something to do with the "foreach( $recent_posts as $recent )" bit, as removing that allows the link to show up on the post, but unfortunately it just links to the same post instead of the most recent. – ZackAkai Jul 27 '20 at 16:39
  • Well I solved part of the mystery, I didn't realize I had a pair of unnecessary brackets in there that was preventing the link from displaying. So it at least now shows up. Still linking to the same page though. – ZackAkai Jul 27 '20 at 16:48
  • I've not used that function before so I had a quick look at the Codex. It looks like you access the data in the object slightly differently to how you've done it, ie. $recent['ID'] as opposed to $recent->ID. In case thats relevant, check the examples at: developer.wordpress.org/reference/functions/wp_get_recent_posts – t2pe Jul 28 '20 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.