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.

My code lists the child pages ordered by post title, pulling through the first image attachment of the child post as the thumbnail.

The child pages contain custom fields created using the Magic Fields plugin.

Can anyone help me adjust my code so that I can have my pages listed in descending order of a custom field called 'price'?

This is my code:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <div class="page" id="post-<?php the_ID(); ?>">

            <div class="pagecontent">


            <?php $child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY post_title", 'OBJECT');   ?>


<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>

<div class="grid"><a href="<?php echo  get_permalink($pageChild->ID); ?>" alt="<?php echo $pageChild->post_title; ?>" title="<?php echo $pageChild->post_title; ?>">
<?php $attachments = get_children( array(
                'post_parent'    => $pageChild->ID,
                'post_type'      => 'attachment',
                'numberposts'    => 1, // show all -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="<?php echo  get_permalink($pageChild->ID); ?>" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></h1>
</div>

<?php endforeach; endif; ?>      

            </div>
        </div>

        <?php endwhile; endif; ?>

I would appreciate any help!

Thank you.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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="<?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="<?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.
share|improve this answer
    
good answer. +1 –  kaiser Mar 29 '11 at 23:31
    
Thank you, kind sir :) –  Ryan Mar 30 '11 at 4:48
    
This is exactly what I needed, it works perfectly. Thank you very much :-) –  Darren Mar 30 '11 at 7:43
    
@Darren, I'm glad it worked for you. Would you mind accepting the answer so future users know it is a good one? –  Ryan Mar 30 '11 at 15:27
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.