Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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>
share|improve this answer
    
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 Mar 15 '11 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 Mar 15 '11 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 Jan 31 '13 at 5:59
    
@RedWolves not true! since you use setup_postdata($myposts[0]); one line above it which sets the $post object. –  Bainternet Jan 31 '13 at 7:17
1  
@RedWolves Ok updated, thanks. –  Bainternet Jan 31 '13 at 15:09
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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