Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm creating a one page wordpress website, and I need my 'home', 'about', 'portfolio', 'services' and 'contact' pages to all be on my front page. I created the following page template for my front page:

page-home.php:

<?php get_header(); ?>

        <div id="primary">

            <div id="content">

                <?php 

                    $titles = array('home', 'about', 'portfolio', 'services', 'contact'); 

                    $ids = array();

                    foreach($titles as $title) {
                        $page = get_page_by_title($title);
                        if($page) {
                            $ids[] = $page->ID;
                        }
                    }

                    global $wp_query;
                    $wp_query = new WP_Query(array('post_type' => 'page', 'post__in' => $ids));

                ?>

                <?php while ( have_posts() ) : the_post(); ?>

                    <?php get_template_part( 'content', 'page' ); ?>

                <?php endwhile; ?>

            </div><!-- #content -->

        </div><!-- #primary -->

        <?php get_sidebar(); ?>

<?php get_footer(); ?>

The problem I have is that the queried pages, $wp_query->posts, aren't ordered by the array of page ids, $ids, that I used to query the pages. How can I sort the $wp_query->posts array against the $ids array?

This is my first question on wordpress.stackexchange.com so if there is a problem with my question, please let me know.

share|improve this question
add comment

2 Answers 2

There is no need to use such custom code when wordpress provides a better way of ordering the pages using menu_order. You can see this option under Page Attributes box on the right side of page edit screen. You can assign the ordering as per your criteria, then use the following code

<?php
global $wp_query;
$wp_query = new WP_Query(
                    array(
                        'post_type' => 'page',
                        'orderby' => 'menu_order',
                        'order' => 'ASC'
                    )
                );
while ( have_posts() ) : the_post(); ?>

    <?php get_template_part( 'content', 'page' ); ?>

<?php endwhile; ?>

Few things to note:

  • As per your ordering of the page titles in the array, you should assign the 1st element the least order and the other elements the higher order as per their sequence.
share|improve this answer
add comment

Worked something out that works:

<?php 

    $titles = array('home', 'about', 'portfolio', 'services', 'contact'); 

    $ids = array();

    foreach($titles as $title) {
        $page = get_page_by_title($title);
        if($page) {
            $ids[] = $page->ID;
        }
    }

    global $wp_query;
    $wp_query = new WP_Query(array('post_type' => 'page', 'post__in' => $ids));

    $sorted_posts = array();
    for($i = 0; $i < count($ids); $i++) {
        for($j = 0; $j < count($wp_query->posts); $j++) {
            if($ids[$i] == $wp_query->posts[$j]->ID) {
                $sorted_posts[] = $wp_query->posts[$j];
            }
        }
    }
    $wp_query->posts = $sorted_posts;

?>

Glad to accept any better answers than this though, so I'll leave the question open to give anyone else the chance. However if there aren't any, then I'll mark this as the accepted answer.

share|improve this answer
3  
Why not just use 'order' => 'menu_order' –  Wyck Nov 25 '12 at 5:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.