0

Why GUID is such unstable? Some of the post's GUID are pretty permalinked, but most of them are default. I's using Pippin's function to get the post_id from its GUID, but it's not working if my site is using pretty permalink, and the GUID is default. But it's working on those posts only, where the GUID is pretty permalinked. I tried using url_to_postid() too - same result.

I'm actually failing doing such redirection where posts that are expired (post_status expired) will be redirected from their 404 page to their respective category page. Any idea on which I can do it successfully?

function prefix_expired_redirection() {
    if( ! is_404() )
        return;

    //Grab the URL
    $this_url   = prefix_get_current_url(); //custom function retrieves pretty permalinked URL

    //If the URL is of a post
    //Pippin's Function: https://pippinsplugins.com/retrieve-attachment-id-from-image-url/
    $post_id    = prefix_get_post_id( $this_url );

    //We've tasks if it's only a post :)
    if( ! $post_id )
        return;

    //Grab the post's terms from 'cpt_tax'
    $terms      = wp_get_post_terms( $post_id, 'cpt_tax' );

    //Grab the first term and its link
    $term_id    = $terms[0]->term_id;
    $term_link  = get_term_link( $term_id, 'cpt_tax' );

    //is_expired_post() custom function - determining expired posts
    if( is_expired_post( $post_id ) ) {
        wp_redirect( esc_url( $term_link ) );
        exit();
    }
}
add_action( 'template_redirect', 'prefix_expired_redirection' );
  • 1
    You should not be using the GUID for this kind of task. It is not a URL : wordpress.stackexchange.com/a/90209/21376 – s_ha_dum Dec 2 '15 at 14:42
  • Beyond that, exactly what are you trying to accomplish? – s_ha_dum Dec 2 '15 at 14:48
  • Some of the posts are getting expired periodically. I'll change their post_status into expired. An expired post won't show up on their designated URL, so the visitor and search engines will get 404. Many 404 will downrank a site, so I need to catch 'em there, and need to redirect somewhere else (i.e. their category page or home page). I mentioned that briefly in the second para. Anyway, I solved it using redirection plugin, and filling their db table with my expired posts. But it'd be fine if the above can function somehow. – Mayeenul Islam Dec 2 '15 at 15:57

Your Answer

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

Browse other questions tagged or ask your own question.