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> <?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!
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):