Tell me more ×
WordPress Answers 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

1 Answer

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
1  
Why not just use 'order' => 'menu_order' – Wyck Nov 25 '12 at 5:47

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.