Grow your CSS skills. Land your dream job.

sorting posts by custom fields

  • # August 16, 2010 at 4:11 pm

    Hello

    I have recently uploaded a wordpress based homepage.
    My client would very much like to be able to sort posts by way of custom fields.

    Here is the specific page:
    http://www.bisgaardlandskab.dk/ny/projekter

    The custom fields are under each post called "type", "størrelse" (size), "årstal" (year).

    Is there anyway I will be able to do this?

    Thanks
    Simon

    # August 17, 2010 at 10:42 pm

    Do you know about WordPress’ query_posts? You’d start with:

    < ?php query_posts("orderby=meta_value&meta_key=type"); ?>
    # August 25, 2010 at 3:59 pm

    thank you

    Well it sort of works. Maybe you can tell me how to make it work as it should.
    What i want is to make a link that makes it possible to sort by e.g “type”

    If I add this:

    < ?php query_posts('meta_key=type'); ?

    it sorts my posts, but how do I make links that sort my posts?
    Of course I need to add some more code for this, but I can’t seem to find the answer anywhere.

    Thanks
    Simon

    # August 25, 2010 at 4:27 pm

    What do you mean by link? Do you mean a few buttons that sort by letter or type or date, etc?

    # August 25, 2010 at 4:32 pm

    yes
    links that makes it possible for visitors to filter the posts

    /Simon

    # August 25, 2010 at 4:56 pm

    You could write your own PHP for this. Perhaps it links you yoursite.com/?sort=whatever

    Then you $_GET, sanitize it, and use that in your query_posts on that page as the meta key. You could pass any number of variables this way.

    # August 25, 2010 at 5:24 pm

    Hi Chris.

    Thank you very much. I am sure that this would work.
    I am just a bit unsure about exactly how to do it. My php skills are quite limited…

    Thanks
    Simon

    # August 26, 2010 at 12:34 pm

    Hello again

    Is there any simple way of describing this to a php noob?

    /Simon

    # September 5, 2010 at 12:46 pm
    < ?php

    $whatever = sanitize($_GET);
    query_posts("meta_key=$whatever");

    function sanitize() {
    // A function to sanitize the input so your database isn't hacked, etc.
    }

    ?>

    I’ll let you find some decent sanitizing code with the google, but that’s basically all you’d have to do.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".