Change Page Order by Custom Field (Magic Fields)

I tested this code and it works great:

<?php query_posts('meta_key=price&orderby=meta_value_num&post_parent=".$post->ID."&post_type=page'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <div class="page" id="post-<?php the_ID(); ?>">
        <div class="pagecontent">
            <div class="grid">
                <a href="https://wordpress.stackexchange.com/questions/13375/<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                    <?php
                        $attachments = get_children( array (
                            'post_parent'    => $post->ID,
                            'post_type'      => 'attachment',
                            'numberposts'    => 1,
                            'post_status'    => 'inherit',
                            'post_mime_type' => 'image',
                            'order'          => 'ASC',
                            'orderby'        => 'menu_order ASC'
                        ) );

                        foreach ( $attachments as $attachment_id => $attachment ) {
                            echo wp_get_attachment_image( $attachment_id, 'medium' );
                        }
                    ?>
                </a>
                <h1><a href="https://wordpress.stackexchange.com/questions/13375/<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
            </div> <!-- // grid -->
        </div> <!-- // pagecontent -->
    </div> <!-- // page -->

<?php endwhile; endif; ?>

A couple of notes:

  • You don’t actually need to use get_results. I combined your custom query restrictions with the query_posts call at the top
  • The query_posts call is ordering by meta_value_num, which is ideal for sorting by numbers. If your “price” field is a string, replace meta_value_num with meta_value.
  • This query will only return posts that have a “price” field defined. That is, if your price field is empty for a post, that post won’t be returned.