0

I'm using the following code to display a list of properties on multiple pages. I would like to give users the option of sorting the results by custom fields (perhaps city, or property name, or price). I've tried add_query_arg without success. Does anyone have any suggestions? I would truly appreciate a code example as I'm fairly new to PHP. Thank you so much.

                <?php
                $wp_query = new WP_Query(array(
                    'meta_query' => array(
                        'relation' => 'AND',
                        array(
                            'key' => 'listing_status',
                            'value' => array('Active', 'In Contract'),
                        ),
                        array(
                            'key' => 'listing_type',
                            'value' => 'For Lease'
                        )
                    ),

                    'paged' => $paged,
                    'orderby' => 'title',
                    'order' => 'ASC'

                ));

                $paged = (get_query_var('paged')) ? get_query_var('paged') : -1;

                ?>
0
                <p>Sort by: <a href="<?php echo str_replace(('?'.$_SERVER['QUERY_STRING']), '', $_SERVER['REQUEST_URI']).'/?sort=name'; ?>">Property Name</a> | <a href="<?php echo str_replace(('?'.$_SERVER['QUERY_STRING']), '', $_SERVER['REQUEST_URI']).'/?sort=type'; ?>">Property Type</a>

                <?php if($_GET['sort'] == 'name') $wp_query = new WP_Query(array( 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'listing_status', 'value' => array('Active', 'In Contract'), ), array( 'key' => 'listing_type', 'value' => 'For Sale' ) ), 'paged' => $paged, 'orderby' => 'meta_value', 'meta_key' => 'property_name', 'order' => 'ASC' )); ?>

                <?php if($_GET['sort'] == 'type') $wp_query = new WP_Query(array( 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'listing_status', 'value' => array('Active', 'In Contract'), ), array( 'key' => 'listing_type', 'value' => 'For Sale' ) ), 'paged' => $paged, 'orderby' => 'meta_value', 'meta_key' => 'property_type', 'order' => 'ASC' )); ?>
  • Explain why that could solve the problem. – fuxia Dec 8 '12 at 0:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.