Order DESC and ASC search results

In searchresults.php

When I select "Price Ascending", it works, but loads all the properties on the page. For example, when I search for Arizona, then select “Price Ascending” from the selection of its non-sequencing properties in Arizona, loading all the properties on the page, then ordering “Price Ascending”.

thank

you can see live on 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);
+3
source share
1 answer

As I understand it, the problem you are facing is that when you select one of the order options on the search results page, ordering is performed, but all entries are displayed on the page, and are not limited to the results of the initial search.

, ( searchresults.php) . "Resultsorder", "formorder". , . , script, search_query.php $_POST $_GET, , . , elseif ($ alllistings) { " search_query.php. , " resultsorder" , script search_query.php , .

, "resultsorder". HTML :

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

, . "resultsorder" . , script, , , , .

, "searchresults.php" - :

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

, - .

+1

Source: https://habr.com/ru/post/1794847/


All Articles