0

Actually I have the problem that I provide a menu link (predefined in theme) to the latest post of a specific custom post type. I achieve this by the following function …

function get_latest_magazine_issue_url() {
    global $wpdb;
    $query = "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type='magazine_issue' AND post_status='publish' ORDER BY post_date DESC LIMIT 1;";
    $result = $wpdb->get_results($query);
    if(is_object($result[0])) { return get_permalink($result[0]->ID); } 
    else { return ''; };
}

… fine til that point – but I recognized, that when I clicked on that link after adding a new cpt post, the link will still grab the post which was the latest when I implemented this link via widget. Is there any way to get around this without a flush_rewrite_rules() call? I wouldn't like to flush that way cause the codex says: http://codex.wordpress.org/Function_Reference/flush_rewrite_rules

2
  • You can't update that link after the page loads without Javascript. Is that what you are trying to do?
    – s_ha_dum
    Commented Feb 7, 2013 at 20:25
  • Wells, yes – I'd like to grab the actually latest post of a cpt, no matter what time and date it is. But I think the drawback in predefing a menu link is, that it get's "hardcoded" as soon as the menu leaves draft status by getting saved via the menu screen. Commented Feb 7, 2013 at 21:01

1 Answer 1

1

Oh, the comment on your question makes it clear. In case you would like to always return a fresh latest post, no matter the page with menu link gets or not refreshed must be solved in another way.

At first: Create new page, let's say: "Latest post" - note it's ID.

And than add this into your functions.php:

function binda_redirect_to_latest( $query ) {
    $page_id = 234; //ID of your page with Latest post
    if ( $query->is_page( $page_id ) && $query->is_main_query() ) {          
        $args = array( 'numberposts' => 1, 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'magazine_issue', 'post_status' => 'publish' ); 
        $latest_post = get_posts( $args ); 
        foreach( $latest_post as $p ){
            $query->set( 'p', $p->ID );
            $query->set( 'page_id', '' );   
        }     
    }
    return $query;
}
add_action( 'pre_get_posts', 'binda_redirect_to_rand' );

Voilá

If you do not want to create an extra page, you can register a new query variable and chceck for it's present in pre_get_posts hook. This way:

add_filter( 'query_vars', 'my_query_vars' );
function my_query_vars( $vars ) {
    $vars[] = 'latest_post';
    return $vars;
}

Modified pre_get_posts hook callback:

function binda_redirect_to_latest( $query ) {    
    if ( $query->is_home() && isset($query->query_vars['latest_post']) && $query->query_vars['latest_post'] == 1 && $query->is_main_query() ) {          
        $args = array( 'numberposts' => 1, 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'magazine_issue', 'post_status' => 'publish' ); 
        $latest_post = get_posts( $args ); 
        foreach( $latest_post as $p ){
            $query->set( 'p', $p->ID );  
        }     
    }
    return $query;
}
add_action( 'pre_get_posts', 'binda_redirect_to_rand' );

To create a ling, you add_query_arg:

echo add_query_arg( 'latest_post', 1, get_bloginfo('url') );
8
  • Hmm… unfortunately this passes the same post as the mentioned method. I'm not using any cache plugin. Damn, thanks anyway! Commented Feb 7, 2013 at 20:56
  • 1
    See modified answer... Commented Feb 7, 2013 at 21:31
  • Hey, thank you very much! This is clearly the working way. The only little drawback in it is that I have to manually create this page and look up its ID after already having installed the theme. Will this also work with let's say the index.php in some way? Commented Feb 8, 2013 at 6:39
  • See extended answer... Commented Feb 8, 2013 at 7:26
  • Unfortunately this returns the common standard posts page... But thank you very much again! Commented Feb 8, 2013 at 11:16

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.