Why aren’t my posts showing?

I’m creating a theme for my portfolio site. After a some time of trial and error research and almostss, I’m stuck.

It’s going to be very minimal site in content, therefore I decided to create a one page website. I’m using get_pages($args); to display each page in my front-page.php file.

This works great. Now here’s where I’m stuck…I’d like to use a page to display my blog posts, which would be included in my one page flow and be called in my WP_Query same as the other pages.

So far I’ve tried the most documented method I can find; which is to change the page that’s used to display posts in Settings>Reading.

To confirm

I have a static home page (front-page.php) which I’ve made a page template declared it in a page called home. Enabling me to set it in the reading settings, and I’ve created a page called blog for the same reason(this has template set to defualt).

enter image description here

My posts are not being loaded into the blog page set here.

I’ve read that a custom query could be interfering but I’ve checked this and it isn’t the case in my situation.

Updated to use WP_Query rather get get_pages

front-page.php

    <?php

        $args = array (
            'post_type' => 'page',
            'order' => 'ASC',
            'orderby' => 'menu_order',
            'posts_per_page' => 5,
        );

        $query = new WP_Query( $args );

        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {
                $query->the_post(); ?>

                <a data-magellan-destination="<?php echo $post->post_name; ?>" name="<?php echo $post->post_name; ?>"></a>
                <div id="<?php echo $post->post_name; ?>" class="page-wrap full">
                    <h2><?php the_title() ?></h2>
                        <?php the_content() ?>
                </div>

            <?php }
        } else {
    }

        wp_reset_postdata();
    ?>

Has anyone come across this type of situation before? Or am I missing something with my loop that would prevent the blog post from loading. I’m learning php and WordPress and hit my limit of experience. I think the call to the content of the blog page itself is preventing the post information to come through? I’m not sure. Any guidance would be appreciated.

Thanks in advance

3 Answers
3

This doesn’t work the way you think it does because get_pages doesn’t do what you think it does.

First, understand that all pages, all content, in WordPress is really a “post”. A “Page” is just a special type of post.

Now, in a normal environment, you wouldn’t call “get_” anything. This is why you’re confused, because you’re directly getting things and thus bypassing everything that WordPress does automatically for you. When you call get_pages, then it gets those pages directly. It doesn’t take the “static home page” setting into account, it simply does what it is told and gets the content of those pages.

See, WordPress automatically parses the URL and determines what it is that should be displayed on that URL. This happens according to the rewrite system. The rewrite rules turn a “pretty” URL into a series of parameters for the WP_Query system. The WP_Query system pulls out the posts to be displayed, and saves them in a global $wp_query. The template system then determines which template to pull from the theme based on the various data in that global WP_Query, and then loads that template. The template implements The Loop, which displays the posts contained within the global $wp_query.

So, to sum up:

  • URL gets processed by rewrites
  • Rewrites creates query parameters
  • Query parameters used to determine posts to get
  • Template system examines query to load proper template
  • Proper template displays the posts from the query

Each step here happens all by itself. You don’t really control it, it’s just what WordPress does at start-up.

The “Static Home Page” setting modifies step three there, where the query is getting the “home page” it has a decision to either get the posts from the blog or to get some static page. Similarly, when you tell it to get a specific Page, if that page is set to be the blog, then the query will get the posts to display there instead. Everything else happens normally.

By doing the get_pages yourself, you’re bypassing everything here and just saying to get these pages and display them, period. Nothing else in the system will impact that because you’ve bypassed it all.

If you want to display everything on a single page, then you need to have it get exactly what you want it to display and then display that. In short, “Static Home Page” doesn’t make the Pages have different content, it just changes what content is chosen by the query to display on those particular pages. If you want to get the posts, then you need to call get_posts in some form, or use a custom WP_Query, or modify the main query, or something else. The get_pages() function cannot really retrieve blog posts. Well, it can, but there’s no point since get_posts() does that instead.

Leave a Comment