Ordering search results by DESC and ASC

In the searchresults.php

When i select “Price Ascending” its working but its loading all properties on to page. For example when i search Arizona then select “Price Ascending” from selection its not ordering properties at Arizona its loading all properties on page then ordering by “Price Ascending”.

Read More

Thanks

you can see live at sedefemlak.com

searchresults.php

                    <div id="resultsorder" style="">
        <form name="formorder" method="POST" action="<?php bloginfo('url'); ?>/?page_id=<?php echo $wp_searchpageid; ?>">
        <select name="resultsorder" onChange="formorder.submit();">
                        <option>Order</option>
                        <option>Date Descending</option>
                        <option>Date Ascending</option>
                        <option>Price Descending</option>
                        <option>Price Ascending</option>
                        <option>Random</option>
                    </select>
        </form>
        </div>

    search_query.php

            if($resultsorder) {
    //get value from order dropdown on search results page
    $resultsorder = $resultsorder;
    } else {
    $resultsorder = get_option('wp_searchorder');
}

    switch ($resultsorder) {
        case "Price Descending":
            $metakey = 'price_value';
            $order = 'DESC';
            $orderby = 'meta_value_num';
            break;
        case "Price Ascending":
            $metakey = 'price_value';
            $order = 'ASC';
            $orderby = 'meta_value_num';
            break;
        case "Date Descending":
            $metakey = '';
            $order = 'DESC';
            $orderby = 'date';
            break;
        case "Date Ascending":
            $metakey = '';
            $order = 'ASC';
            $orderby = 'date';
            break;
        case "Random":
            $metakey = '';
            $order = '';
            $orderby = 'rand';
            break;
    }


if (!empty($_ids) && !$alllistings) {

    $wpq = array ('post_type' => 'listing', 'meta_key' => $metakey, 'orderby' => $orderby, 'order' => $order, 'post__in' => $_ids,  'post_status' => 'publish', 'paged' => $paged, 'posts_per_page' => 9999 );

} elseif (empty($_ids) && !$alllistings) {

    // $_ids array is empty because search got no results
    // $_ids array will be empty if page is an "All Listings" page. Don't run this code if is All Listings because All Listings will show all listings. This code will display "no results found"
    $wpq = array ('post_type' =>'listing', 'meta_key' => $metakey, 'orderby' => $orderby, 'order' => $order, 'post__in' => array('0'),'post_status' => 'publish', 'paged' => $paged, 'posts_per_page' => 9999);
} elseif ($alllistings) {
    // This is an All Listings page, so show all results
    $wpq = array ('post_type' =>'listing', 'paged' => $paged, 'meta_key' => $metakey, 'orderby' => $orderby, 'order' => $order, 'post_status' => 'publish', 'posts_per_page' => 9999);
}

$listing = new WP_Query($wpq);

Related posts

Leave a Reply

1 comment

  1. As I understand it, the problem you’re having is that when you choose one of the ordering options from a search results page, the ordering works but the page displays all records instead of being limited to the results of the original search.

    This is happening because your “formorder” form (in the posted searchresults.php file) does not pass along any of the search parameters when it is submitted. When the user chooses an option from the “resultsorder” select box, the “formorder” form is submitted. But that form submits with only one piece of data, the sort order. So the script, search_query.php, is not recieving any $_POST or $_GET data containing information about what the original search was. This means that the final “elseif ($alllistings) {” clause in search_query.php is reached. In other words, with the submission of the “resultsorder” form, the original search data is lost, and your script in search_query.php proceeds as if it has a request to display all listings.

    What you need to do is include the original search parameters as hidden tags in the “resultsorder” form. The HTML for hidden fields looks like this:

    <input type="hidden" id="searchLocation" name="searchLocation" value="California" />
    

    Hidden fields allow you to pass data to your scripts when a form is submitted, without the user seeing the data displayed on your page. This lets your “resultsorder” form pass along the current search parameters along with the chosen sort order. This way your script, when it applies the sort order, will also have data to limit its results to what was originally displayed.

    So inside “searchresults.php” you should have something like this:

    <form name="formorder" method="POST" [ETC]>
        <select name="resultsorder" onChange="formorder.submit();">
            [OPTIONS]
        </select>
        <input type="hidden" name="location_level1" id="location_level1" 
            value="<?php echo $location_level1; ?>" />
        <input type="hidden" name="beds" id="beds" value="<?php echo $beds; ?>" />
    </form>
    

    And so on, with however many hidden fields you need to include all of your search parameters.