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'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;

                ?>
share|improve this question
add comment

1 Answer

                <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' )); ?>
share|improve this answer
    
Explain why that could solve the problem. –  toscho Dec 8 '12 at 0:24
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.