I’m not sure what i’m doing wrong, been researching for the past few days but hopeless.

GOAL: to replace the query so it takes ALL front page data including the following:

  • front page post content
  • front page title
  • front page metadata

and to display the content, title, metadata in a natural way by calling in theme page template

// Takes front page title
<?php wp_title(); ?>

// Takes front page config
<?php wp_head(); ?> 

// takes front page post and display content
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <?php echo the_content(); ?>
        <?php endwhile; endif; ?>

Target URL: homepage.com/amp (not existing page)

Current Code

function front_page_post_AMP( $query  ) {

    // Get's the Current Browser URL
    global $wp;
    $current_url = home_url(add_query_arg(array(),$wp->request));

    // Homepage AMP URL
    $front_page_amp_url = get_site_url() . "/amp";

    // check if the current browser URL is homepage.com/amp
    if ( strcasecmp( $current_url, $front_page_amp_url ) == 0 && $query->is_main_query() ) 
    {

        // gets front page id
        $front_page_id = get_option( 'page_on_front' );

        // replace query id
        $query->set( 'page_id', $front_page_id );

        return $query;
    }
}

add_action( 'pre_get_posts', 'front_page_post_AMP' ); 

Current Results:

  • Page Title = not found
  • Page post content = empty/null

Read more here: WordPress AMP – changing Query on specific URL and not existing page


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: