4

Stupid question, I bet: I'm doing a website for an artist where I have a custom post type of "Painting". How do I get the permalink of the one added most recently?

1 Answer 1

6

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="<?php echo Get_most_recent_permalink(); ?>">last painting</a>
6
  • Well, thanks for trying but not working as given. Here's the thing: I'm using in an iframe and it keeps giving me (when I use the above code) the PARENT url in the place where I want the most recent painting. That is, the URL in the address field, the current URL. Which is bound to be a problem as it will have itself inside itself, etc.
    – user3960
    Commented Mar 15, 2011 at 18:38
  • what??? this function works in one manner, create a pastebin of your code or update the question with it so I'll get a better look and understanding.
    – Bainternet
    Commented Mar 15, 2011 at 19:26
  • $permalink = get_permalink($post->ID); should be $permalink = get_permalink($myposts[0]->ID); This is why user3960 was getting the currentURL cause the latest URL is the one he working on and publishing. $myposts is the filter posts based on the args. I'll update the reply with this correction.
    – RedWolves
    Commented Jan 31, 2013 at 5:59
  • @RedWolves not true! since you use setup_postdata($myposts[0]); one line above it which sets the $post object.
    – Bainternet
    Commented Jan 31, 2013 at 7:17
  • Well it's not doing what you expect! It doesn't work as you have it.
    – RedWolves
    Commented Jan 31, 2013 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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