Treehouse: Grow your CSS skills. Land your dream job.

sorting posts by custom fields

  • # August 16, 2010 at 4:11 pm


    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:

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

    Is there anyway I will be able to do this?


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


    # 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

    links that makes it possible for visitors to filter the posts


    # August 25, 2010 at 4:56 pm

    You could write your own PHP for this. Perhaps it links you

    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…


    # August 26, 2010 at 12:34 pm

    Hello again

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


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

    $whatever = sanitize($_GET);

    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.