• em_jay

    (@em_jay)


    Hi,

    with the following function and the template_redirect hook, i’m redirecting a parent page to first child page based on menu_order. This is working fine on all pages, except the startpage where I’m redirect to by polylang.

    In this case, I get the error ‘Trying to get property of non-object)’ because of $post isn’t set.

    Is there something different on the Polylang startpage? If I’m accessing domain.com/en/startpage, it is working fine. If I’m accessing domain.com then polylang is redirecting to domain.com/en/ and it is not working.

    I thought domain.com/en/ is the same like domain.com/en/startpage. (I’ve chosen to display the front page as a static page in Settings > Reading)

    function weParentPageRedirect() {

    global $post;

    // get child pages
    $child_page = get_pages( “child_of=” . $post->ID . “&sort_column=menu_order” );

    if ( $child_page ) {
    // get id of first child page
    $firstchild = $child_page[0];
    wp_redirect( get_permalink( $firstchild->ID ), 301 );
    exit();
    }

    }
    add_action( ‘template_redirect’, ‘weParentPageRedirect’ );

    Thanks!

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

    (@em_jay)

    The function get_queried_object returns the following:

    On the page domain.com/en/startpage where it is working:

    WP_Post Object
    (
        [ID] => 6
        [post_author] => 1
        [post_date] => 2016-10-28 16:39:54
        [post_date_gmt] => 2016-10-28 14:39:54
        [post_content] => 
        [post_title] => Startpage
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => closed
        [ping_status] => closed
        [post_password] => 
        [post_name] => startpage
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2016-11-05 21:58:58
        [post_modified_gmt] => 2016-11-05 20:58:58
        [post_content_filtered] => 
        [post_parent] => 0
        [guid] => https://domain.com/?page_id=6
        [menu_order] => 1
        [post_type] => page
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )

    On the page domain.com/en/ where it is not working:

    WP_Term Object
    (
        [term_id] => 6
        [name] => English
        [slug] => en
        [term_group] => 0
        [term_taxonomy_id] => 6
        [taxonomy] => language
        [description] => a:3:{s:6:"locale";s:5:"en_EN";s:3:"rtl";i:0;s:9:"flag_code";s:2:"en";}
        [parent] => 0
        [count] => 42
        [filter] => raw
    )
    • This reply was modified 8 years ago by em_jay.
Viewing 1 replies (of 1 total)
  • The topic ‘is the polylang startpage i’m redirected to, different then other pages?’ is closed to new replies.