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 operate a real estate website, and I have a list of homes we are selling. I am using a bunch of custom fields to store information about the homes. When you click on a home in the home listing page (page, not post), it takes you to a dedicated page (again, not post) for that home. I'm very new to PHP and Wordpress.

Right now the code I am using (get_pages) is working to display all the pages I want it to, but it is sorting by the post_date. Instead, what I want it to do is sort alphabetically by one of my Custom Field meta values; specifically, to sort A-Z by the street address. Is there any way I can use the "sort_column" parameter to do this? Everything I've found from searching has been related to getting posts instead of pages.

Here is my current code that gets the pages:

<?php $listingPages = get_pages( 
        array(
            'sort_order' => 'ASC',
            'sort_column' => 'post_date',
            'meta_key' => 'houseNo',
            'post_type' => 'page',
            'post_status' => 'publish'
        )
    );

    foreach( $listingPages as $page ) {     
        $content = $page->post_content;
        if ( ! $content ) // Check for empty page
            continue;

        $content = apply_filters( 'the_content', $content );
    ?>
        <div class="listingPreview">
            <h2 class="listingTitle"><a href="<?php echo get_page_link( $page->ID ); ?>"><span class="houseNo"><?php echo get_post_meta($page->ID, 'houseNo', true); ?></span>&nbsp;<?php echo get_post_meta($page->ID, 'streetAdd', true); ?></a></h2>
        </div>
    <?php
    }
?>

Please excuse my dirty code, I'm a complete noob and am totally on my own with this and have no professional training.

So basically I'm wondering if I could do something like this with the sort_column parameter (this obviously doesn't work):

<?php $listingPages = get_pages( 
        array(
            'sort_order' => 'ASC',
            'sort_column' => get_post_meta('streetAdd'),
            'meta_key' => 'houseNo',
            'post_type' => 'page',
            'post_status' => 'publish'
        )
    );

I don't even know if I'm asking the right question, or if I need to use some sort of custom query called from functions.php or something. I'm open to whatever I need to do to achieve this. Thank you for your help!

share|improve this question
    
Welcome to WordPress Answers! Have you tried looking at the Codex page for get_post_meta or WP_Query? You can probably answer your own question by looking there. –  Brian Fegter Sep 23 '12 at 19:19
add comment

1 Answer

up vote 2 down vote accepted

The get_pages function is rather old and really shouldn't be used anymore. Use a get_posts instead (a Page is just a special type of Post):

$listingPages = get_posts( 
    array(
        'meta_key' => 'streetAdd',
        'post_type' => 'page',
        'post_status' => 'publish',
        'orderby' => 'meta_value',
        'order' => 'ASC',
    )
);
share|improve this answer
    
Okay that worked; one caveat though. It took me a minute to figure out that the default post count is 5, so I had to add in the parameter 'numberposts' => 100,. Is there any way to make the numberposts infinite? –  redsun Sep 7 '12 at 20:17
    
Use posts_per_page instead of numberposts, and set it to -1. –  Otto Sep 7 '12 at 20:20
    
Perfect, works like a charm. Thanks for your help! –  redsun Sep 7 '12 at 20:30
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.