WordPress.org

Ready to get started?Download WordPress

Forums

Help with a secondary sort function using custom fields (2 posts)

  1. tamerax
    Member
    Posted 1 year ago #

    Hey all :)

    I'm trying to setup a my page to do a secondary sort by price of the property listings as shown here: http://apmottawa.com/property/?post_type=property&search_keyword=&submit=Search&price-min=&price-max=&beds=&baths=&sqft=&ptype=&status=For+Rent&sort=date

    Now I had someone setup the page to display the property by grouping them by available date which is a custom field but he couldn't figure out how to then sort those results by price from highest to lowest within each month. I'm not familiar enough with how the args in the loop work so I'm hoping someone here can take a look at this and help. I know the meta field to do the secondary sort is pyre_price and $args['meta_query'][] = array('key' => 'pyre_date', 'value' => $v, 'compare' => 'IN', 'type' => 'char'); is the line that needs to be manipulated.

    Here is the full code from the page.

    Thanks so much!!

    <?php
    
    get_header(); 
    
    $purl = get_post_type_archive_link('property');
    
    function my_filter_posts_orderby( $orderby )
    {
        global $wpdb;
        $orderby = $wpdb->postmeta . '.meta_value ASC, ' . $orderby;
        return $orderby;
    }
    
    ?> <script type="text/javascript"> function submit_sorting(val) { var url = '<? echo "/property/?" . $_SERVER['QUERY_STRING']; ?>';
    
            var pos = url.indexOf("&sort="); 
    
            if(pos != -1)
            {
                var url = url.substring(0, pos);
            }
    
            if(val != '')
            {
                url = url + "&sort=" + val;
            }
    
            window.location.href = url;
        }
    </script>
    
    <div id="main">
        <div class="width-container">
            <div id="container-sidebar">
                <div class="content-boxed">
                    <h2 class="title-bg"><?php echo of_get_option('search_results_text', 'Search Results'); ?></h2>
                    <div id="sortable-search">
                        <select name="sorting" id="sorting" onchange="submit_sorting(this.value);">
                            <option value="recent" <?php if($_GET['sort'] == 'recent') { ?>selected="selected"<? } ?>>Most Recent</option>
                            <option value="high" <?php if($_GET['sort'] == 'high') { ?>selected="selected"<? } ?>>Price (high to low)</option>
                            <option value="low" <?php if($_GET['sort'] == 'low') { ?>selected="selected"<? } ?>>Price (low to high)</option>
                            <option value="date" <?php if(!isset($_GET['sort']) || $_GET['sort'] == 'date') { ?>selected="selected"<? } ?>>Availabilty Date</option>
                        </select>
                    </div>
    
    <?php if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) { $paged = get_query_var('page'); } else { $paged = 1; }
    
                    $args = array('post_type' => 'property', 'paged' => $paged);
    
                    if(isset($_GET['sort']))
                    {
                        if($_GET['sort'] == 'recent')
                        {
                            $args['orderby'] = 'posts_date';
                            $args['order'] = 'DESC';
                        }
                    }
    
                    if(isset($_GET['sort']))
                    {
                        if($_GET['sort'] == 'high')
                        {
                            $args['meta_key'] = 'pyre_price';
                            $args['orderby'] = 'meta_value_num';
                            $args['order'] = 'DESC';
                        }
                    }
    
                    if(isset($_GET['sort']))
                    {
                        if($_GET['sort'] == 'low')
                        {
                            $args['meta_key'] = 'pyre_price';
                            $args['orderby'] = 'meta_value_num';
                            $args['order'] = 'ASC';
                        }
                    }
    
                    if(!isset($_GET['sort']) || $_GET['sort'] == 'date')
                    {
                        $cur_month = date('n');
                        //$cur_month = 4;
    
                        $month_str = "";
    
                        $zero_str = "";
    
                        if($cur_month == 1)
                        {
                            $j = $cur_month + 1;
    
                            for($i=$j; $i<=12; $i++)
                            {
                                if($i < 10)
                                {
                                    $zero_str = "00";
                                }
                                else
                                {
                                    $zero_str = "0";
                                }
    
                                $month_str .= $zero_str . $i . ",";
                            }
    
                            $month_str = "000," . $month_str;
    
                            $v = substr($month_str, 0, (strlen($month_str) - 1));
                        }
    
                        if($cur_month > 1 && $cur_month < 12)
                        {
                            $j = $cur_month + 1;
    
                            for($i=$j; $i<=12; $i++)
                            {
                                if($i < 10)
                                {
                                    $zero_str = "00";
                                }
                                else
                                {
                                    $zero_str = "0";
                                }
    
                                $month_str .= $zero_str . $i . ",";
                            }
    
                            $k = ($cur_month - 1);
    
                            for($i=1; $i<=$k; $i++)
                            {
                                if($i < 10)
                                {
                                    $zero_str = "00";
                                }
                                else
                                {
                                    $zero_str = "0";
                                }
    
                                $month_str .= $zero_str . $i . ",";
                            }
    
                            $month_str = "000," . $month_str;
    
                            $v = substr($month_str, 0, (strlen($month_str) - 1));
                        }
    
                        if($cur_month == 12)
                        {
                            for($i=0; $i<12; $i++)
                            {
                                if($i < 10)
                                {
                                    $zero_str = "00";
                                }
                                else
                                {
                                    $zero_str = "0";
                                }
    
                                $month_str .= $zero_str . $i . ",";
                            }
    
                            $v = substr($month_str, 0, (strlen($month_str) - 1));
                        }
    
                        $args['meta_query'][] = array('key' => 'pyre_date', 'value' => $v, 'compare' => 'IN', 'type' => 'char');
    
                        //$args['meta_key'] = 'pyre_date';
                        //$args['orderby'] = 'meta_value';
                        //$args['orderby'] = 'menu_order';
                        //$args['order'] = 'ASC';
                    }
    
                    if(isset($_GET['status']))
                    {
                        if($_GET['status'])
                        {
                            $args['meta_query'][] = array('key' => 'pyre_status', 'value' => $_GET['status'],);
                        }
                    }
    
                    if(isset($_GET['price-min']))
                    {
                        if($_GET['price-min'] >= 1)
                        {
                            $args['meta_query'][] = array('key' => 'pyre_price', 'value' => $_GET['price-min'], 'compare' => '>=', 'type' => 'numeric');
                        }
                    }
    
                    if(isset($_GET['price-max']))
                    {
                        if($_GET['price-max'] >= 1)
                        {
                            $args['meta_query'][] = array('key' => 'pyre_price', 'value' => $_GET['price-max'], 'compare' => '<=', 'type' => 'numeric');
                        }
                    }
    
                    if(isset($_GET['city']))
                    {
                        if($_GET['city'])
                        {
                            $args['meta_query'][] = array('key' => 'pyre_city', 'value' => $_GET['city'],);
                        }
                    }
    
                    if(isset($_GET['state']))
                    {
                        if($_GET['state'])
                        {
                            $args['meta_query'][] = array('key' => 'pyre_state', 'value' => $_GET['state'],);
                        }
                    }
    
                    if(isset($_GET['zip']))
                    {
                        if($_GET['zip'])
                        {
                            $args['meta_query'][] = array('key' => 'pyre_zip', 'value' => $_GET['zip'],);
                        }
                    }
    
                    if(isset($_GET['date']))
                    {
                        if($_GET['date'])
                        {
                            $args['meta_query'][] = array('key' => 'pyre_date', 'value' => $_GET['date'],);
                        }
                    }
    
                    if(isset($_GET['beds']))
                    {
                        if($_GET['beds'] >= 1)
                        {
                            $args['meta_query'][] = array('key' => 'pyre_bedrooms', 'value' => $_GET['beds'], 'compare' => '>=', 'type' => 'numeric');
                        }
                    }
    
                    if(isset($_GET['baths']))
                    {
                        if($_GET['baths'] >= 1)
                        {
                            $args['meta_query'][] = array('key' => 'pyre_bathrooms', 'value' => $_GET['baths'], 'compare' => '>=', 'type' => 'numeric');
                        }
                    }
    
                    if(isset($_GET['sqft']))
                    {
                        if($_GET['sqft'] >= 1)
                        {
                            $args['meta_query'][] = array('key' => 'pyre_size', 'value' => $_GET['sqft'], 'compare' => '>=', 'type' => 'numeric');
                        }
                    }
    
                    if(isset($_GET['ptype']))
                    {
                        if($_GET['ptype'])
                        {
                            $args['tax_query'][] = array('taxonomy' => 'property_type', 'field' => 'slug', 'terms' => $_GET['ptype']);
                        }
                    }
    
                    if(isset($_GET['search_keyword']))
                    {
                        if($_GET['search_keyword'])
                        {
                            $args['meta_query'] = array();
                            $args['meta_query'][] = array('key' => 'pyre_full_address', 'value' => $_GET['search_keyword'], 'compare' => 'LIKE', 'type' => 'CHAR');
                        }
                    }
    
                    add_filter('posts_orderby', 'my_filter_posts_orderby');
                    query_posts($args);
                    remove_filter('posts_orderby', 'my_filter_posts_orderby');
    
                    if(have_posts()):
    
    //removed google map script to condense this post
    
    <?php while(have_posts()): the_post(); get_template_part( 'property-listing' ); // Navigation bar (property-listing.php) endwhile;
    
                    else:
    
    ?> <h3><?php echo of_get_option('search_results_none_title', 'No properties were found which match your search criteria.'); ?></h3> <p><?php echo of_get_option('search_results_none_content', 'Try broadening your search to find more results.'); ?></p> <?php endif; ?> <div class="clearfix"></div> </div><!-- close .content-boxed -->
    
                <div class="clearfix"></div>
                <?php if($wp_query->max_num_pages): ?>
                    <div style="text-align:center;">
                        <div class="page-count"><?php _e('Page','progressionstudios'); ?> <?php echo $paged; ?> <?php _e('of','progressionstudios'); ?> <?php echo $wp_query->max_num_pages; ?></div>
                        <?php kriesi_pagination($wp_query->max_num_pages, $range = 2); ?>
                    </div>
                <?php endif; ?>
                <div class="clearfix"></div>
    
            </div><!-- close #container-sidebar -->
            <?php wp_reset_query() ?>
            <?php include 'sidebar-real-estate.php'; ?>
            <div class="clearfix"></div>
        </div><!-- close .width-container -->
    </div><!-- close #main -->
    
    <?php get_footer(); ?>
  2. tamerax
    Member
    Posted 1 year ago #

    anyone?

Topic Closed

This topic has been closed to new replies.

About this Topic