WordPress.org

Ready to get started?Download WordPress

Forums

Sort by drop-down with custom field value (29 posts)

  1. meko6
    Member
    Posted 3 years ago #

    I'm using the following to add a drop-down which sorts posts by title, oldest and newest. I want to add an option to the drop down that queries a price (custom field name is rw_app_price). The following is what I have so far ...

    <?php
      $order = "&orderby=cost&order=DESC";
      $s2 = ' selected="selected"';
      if ($_POST['select'] == 'title') { $order = "&orderby=title&order=ASC"; $s1 = ' selected="selected"'; $s2 = ''; }
      if ($_POST['select'] == 'newest') { $order = "&orderby=cost&order=DESC"; $s2 = ' selected="selected"'; }
      if ($_POST['select'] == 'oldest') { $order = "&orderby=cost&order=ASC"; $s3 = ' selected="selected"'; $s2 = ''; }
    ?>
    
    <form method="post" id="order">
      Sort reviews by:
      <select name="select" onchange='this.form.submit()'>
        <option value="title"<?=$s1?>>Title</option>
        <option value="newest"<?=$s2?>>Newest</option>
        <option value="oldest"<?=$s3?>>Oldest</option>
      </select>
    </form>
    
    		<?php if (have_posts()) : ?>
        <?php $posts = query_posts($query_string . $order); ?>
        <?php while (have_posts()) : the_post(); ?>
  2. pbul2004
    Member
    Posted 2 years ago #

    Did you have any luck with this mate?

    Im after doing the exact same thing, to sort posts by:-

    ASC
    DESC
    # Comments
    TAG.

    Did you manage to do this, im stuck!

    Cheers
    Paul

  3. keesiemeijer
    moderator
    Posted 2 years ago #

    Did you try the code? I changed it a little bit. Try it with this before a loop you want sorted: http://pastebin.com/LCFS945E

  4. pbul2004
    Member
    Posted 2 years ago #

    Cheers, ill try the code now thanks,

    This is a bit more info on what im after: -

    I want to be able to display a drop down list of tags on the archives.php file to enable a sort/filter on a list of posts.

    For example, i have 20 posts, each 5 posts have a tag of "Cheshire", 10 have a tag of "London" and the other 5 have a tag of "Kent".

    The tag = an Area.

    I want to be able to select tags (areas) that are only active on the posts in a drop down list.

    So there are only Cheshire, London and Kent. I dont want to display the other 30+ different areas. Only the areas / tags that are asigned to the posts listed.

    Then when they select one of the tags such as Kent, it will then display posts only for Kent?

    So what im after is a sort function in a dropdown for active tags on the post lists page?

  5. keesiemeijer
    moderator
    Posted 2 years ago #

    So, you want the tags from the posts that are normally displayed on an archive page in a dropdown?

  6. pbul2004
    Member
    Posted 2 years ago #

    Yeah just the tags in a dropdown so i can filter the posts by area.

    This might give you a better idea of what im trying to do: -
    www dot myweddingreviews dot co dot uk/photographers

    Cheers

  7. pbul2004
    Member
    Posted 2 years ago #

    anyone? please :-)

  8. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with multiple loops. the first loop to get all the post tags and sort them alphabetically. The second loop to show the posts and to sort them by area (if one was selected).

    example: http://pastebin.com/Q6iaUKi5

  9. pbul2004
    Member
    Posted 2 years ago #

    Hi mate,

    Nearly working, the site seems to crash when i include the second loop at the end: -

    <!-- second loop -->
    <?php if ( have_posts() ) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
    <!-- rest of your loop -->

    Can see what its like so far: -
    www dot myweddingreviews dot co dot uk/photographers

    Everything above that works, but when i include that 2nd loop my site doesnt display.

    The code for the posts i have is: -

    [Code moderated as per the Forum Rules. Please use the pastebin]

  10. keesiemeijer
    moderator
    Posted 2 years ago #

    From your loop remove:

    <?php query_posts($query_string . '&orderby=rand');?>

    From my code remove at the end:

    <!-- second loop -->
    <?php if ( have_posts() ) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
    <!-- rest of your loop -->

    I didn't include a second loop in my code. Just the beginning of a loop.

  11. pbul2004
    Member
    Posted 2 years ago #

    do i need to do something after the 2nd loop? Not good at php, but im guessing thats why its not working when i select an area from the drop down?

    Or is it a conflict from the code i have pasted above?

  12. pbul2004
    Member
    Posted 2 years ago #

    Yesssssssssssssss!!!

    Brilliant, thank you so much! Awesome.

    Im guessing this would work a similar way if i wanted to add another fliter such as categories ie: Photographer, Wedding Cakes, Dresses etc?

    MUCH APPRECIATED! :-)

  13. keesiemeijer
    moderator
    Posted 2 years ago #

    Can you paste and submit the full code of the template file with my code in it into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.

  14. keesiemeijer
    moderator
    Posted 2 years ago #

    It takes the current category and gets the tags from the posts displayed (so not all tags). So you can use it for Wedding Cakes, Dresses etc. If the tags are always "area's".

  15. pbul2004
    Member
    Posted 2 years ago #

    Here is the link to the archives.php file: -

    http://pastebin.com/WAHLgVPb

    The area uses tags, which is working thanks!

    And the wedding topic uses "category" in wordpress.

    So photographers, dresses, cakes, etc are all categories.

    Thanks :-)

  16. keesiemeijer
    moderator
    Posted 2 years ago #

    If it's a (any) category Page it will work.

    The only downside of doing it this way is that it doesn't work for people who have javascript turned off.

    I hope this resolves it for you.

  17. pbul2004
    Member
    Posted 2 years ago #

    yeah any category on any of the pages, so if your on photographers, you can change the category to something else like dresses, and filter only businesses for dresses. Then they can use the tag filter and filter by area.

    i think if js was turned off nothing would work lol.

    I'll give it a try mate,

    thanks for your help!!! :-)

  18. keesiemeijer
    moderator
    Posted 2 years ago #

    A quick (and dirty) workaround is to not show the form when javascript is turned off.
    put this in your theme's stylesheet (style.css):

    #tag_order {
    display: none;
    }

    and this in your theme's functions.php:

    add_action('wp_footer', 'tag_order');
    
    function tag_order(){
    $script = '';
    if(!is_admin()) {
    $script = '<script type="text/javascript">
    var form = document.getElementById(\'tag_order\');
    form.style.display = \'block\';
    </script>';
    }
    echo $script;
    }

    this requires your theme to have <?php wp_footer(); ?> in the footer.php (just before the </body> tag).

  19. pbul2004
    Member
    Posted 2 years ago #

    thanks for that, ill apply that in a sec.

    Might just be me but the Area filter doesnt seem to work in IE or Chrome, i was testing in Firefox at work and that one was ok?

  20. keesiemeijer
    moderator
    Posted 2 years ago #

    What doesn't work? Does it put the order query at the end of the url: /category/photographers/?area=cumbria ?

    The rest is all html and php so it should work on all browsers. Try clearing your browsers cache.

  21. keesiemeijer
    moderator
    Posted 2 years ago #

    Try changing both:

    window.location =

    to

    window.location.href =
  22. pbul2004
    Member
    Posted 2 years ago #

    no luck mate.

  23. keesiemeijer
    moderator
    Posted 2 years ago #

    What doesn't work? Does it put the order query at the end of the url: /category/photographers/?area=cumbria ?

  24. pbul2004
    Member
    Posted 2 years ago #

    In Chrome when you select an area - it doesn't put anything at the end of the URL. It just doesn't seem to load or try to do anything.

    Whereas firefox it works perfectly.

  25. pbul2004
    Member
    Posted 2 years ago #

    Looks like Chrome is missing the "/" from the URL. I used "Inspect Element Tools" and it shows:

    <option value="cheshire" onclick="window.location = " http:="" www. myweddingreviews.co.uk="" category="" health-beauty="" beauty-salons="" ?area="cheshire";" return="" false;"="">Cheshire</option>

  26. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with this: http://pastebin.com/fNZTKXQe

  27. pbul2004
    Member
    Posted 2 years ago #

    yes sorted now, thank you once again, really appreciate your help and time.

    Thanks

  28. keesiemeijer
    moderator
    Posted 2 years ago #

    No problem. Glad you got it resolved.

  29. pbul2004
    Member
    Posted 2 years ago #

    Hi,

    Been trying for a week or so now, an i cant seem to get it to work for the filter by category?

    Filter by tag (area) is working great. But could do with an option when searching in WordPress when bringing back all posts to filter by category as well: -

    Photograhper, Video, Cakes, Dresses etc?

Topic Closed

This topic has been closed to new replies.

About this Topic