• Resolved borimrr

    (@borimrr)


    I’m working on a car dealership website and I managed to get the posts to display in numeric order by a custom field of “price” either ASC or DESC. The problem is I add this code to the header.php template, but I can only display them in order once depending if I choose ASC or DESC:

    <?php query_posts($query_string. '&orderby=meta_value&meta_key=price&order=DESC' ); ?>

    I want to make a “Price” button that when people click on it will toggle the order of the posts based on the price custom field. I know this is hard since it would be mixing preloaded php with dynamic content, but maybe someone knows of a way to accomplish this possibly with a plugin. Thanks in advance.

Viewing 1 replies (of 1 total)
  • Thread Starter borimrr

    (@borimrr)

    So I found a solution after doing some thinking, googling and putting my noob PHP coding skills to work. I used cookies to determine if the posts are already in DESC or ASC order according to their price. Then, the “change price order” link refreshes the page and depending on the cookie will either display the posts in DESC or ASC order accroding to the price meta value.

    Here’s the PHP that goes right after the HTML head ends:

    </head>
    
    <?php
    
    setcookie("priceorder", "ascending");
    
    if($_GET["action"]=="changepriceorder" && $_COOKIE["priceorder"] == 'ascending')
    {
    query_posts($query_string. '&orderby=meta_value&meta_key=price&order=DESC' );
    
    setcookie("priceorder", "descending");
    
    } else if ($_GET["action"]=="changepriceorder" && $_COOKIE["priceorder"] == 'descending') {
    
    query_posts($query_string. '&orderby=meta_value&meta_key=price&order=ASC' );
    
    setcookie("priceorder", "descending");
    }
    
    ?>

    And the simple link that activates the changes:
    <a href="?action=changepriceorder">Price<img src="up_and_down_arrows.gif" /></a>

Viewing 1 replies (of 1 total)
  • The topic ‘How do I sort posts by a custom field dynamically? Plugin?’ is closed to new replies.