Custom Post Type: Get most recent permalink

Here is a simple function that will return the Permalink of the last painting post:

function Get_most_recent_permalink(){
    global $post;
    $tmp_post = $post;
    $args = array(
        'numberposts'     => 1,
        'offset'          => 0,
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'post_type'       => 'Painting',
        'post_status'     => 'publish' );
    $myposts = get_posts( $args );
    $permalink = get_permalink($myposts[0]->ID);
    $post = $tmp_post;
    return $permalink;
}

so once you paste that function in your theme’s functions.php file you can just call it whenever you want:

<a href="https://wordpress.stackexchange.com/questions/12143/<?php echo Get_most_recent_permalink(); ?>">last painting</a>

Leave a Comment