• Hi,

    I am working on a script to redirect a WordPress post to a new site. I’m moving an individual WordPress blog to a WordPress network. On the network site the permalink structure is changing like this:

    Old Permalink: https://www.example.com/YYYY/MM/the-permalink-link.html/

    New Permalink: https://www.newdomain.com/example/YYYY/MM/DD/the-permalink-link/

    The script I’m running will grab the post date and break it up so I can get the DD. I’m running it before the open <html> tag. Here is the script:

    <?php
    
    $post_obj = $wp_query->get_queried_object();
    $post_ID = $post_obj->ID;
    $post_title = $post_obj->post_title;
    $post_slug = $post_obj->post_name;
    
    $post_date = $wpdp->get_results("SELECT post_date FROM wp_posts WHERE post_title = '$post_slug'");
    
    list($y, $m, $d) = explode('-', substr($post_date, 0, 9));
    
    $new_url = 'https://www.rantsports.com/TEAM-NAME/'.$y.'/'.$m.'/'.$d.'/'.$post_slug.'/';
    
    Header( "HTTP/1.1 301 Moved Permanently" );
    Header( "Location: ".$new_url."" ); 
    
    ?>

    And I’m getting the following error:

    Fatal error: Call to a member function get_results() on a non-object in /home/letsgetm/public_html/wp-content/themes/lgm_rantsports/single.php on line 8

    Thanks,
    Grant

Viewing 1 replies (of 1 total)
  • Thread Starter gbrown88

    (@gbrown88)

    Ok, got the script working but its not returning the right link:

    <?php
    
    define( 'BLOCK_LOAD', true );
    require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php' );
    require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php' );
    
    $post_obj = $wp_query->get_queried_object();
    $post_ID = $post_obj->ID;
    $post_title = $post_obj->post_title;
    $post_slug = $post_obj->post_name;
    
    $post_date = $wpdb->get_results("SELECT post_date FROM $wpdb->posts WHERE post_name = '$post_slug'");
    
    list($y, $m, $d) = explode('-', substr($post_date, 0, 9));
    
    $new_url = 'https://www.example.com/TEAM-NAME/'.$y.'/'.$m.'/'.$d.'/'.$post_slug.'/';
    
    Header( "HTTP/1.1 301 Moved Permanently" );
    Header( "Location: ".$new_url."" ); 
    
    ?>

    The result I got was:

    https://www.example.com/TEAM-NAME/Array/thoughts-from-england/

Viewing 1 replies (of 1 total)
  • The topic ‘PHP Redirect Script Not Working’ is closed to new replies.