Quick Query Question - and some alliteration.

I don't know if this out of the realm of support, but i'll go ahead and ask and see.

I have a page that displays the most recent 12 posts with a Featured Image. Using this query


I've added a Custom Field to posts called

And i'd like to sort by this field in the query above. Can someone show me how to update the query code please?

Thanks :slight_smile:


  • Alexander
    • DEV MAN’s Mascot

    Hi @Revealwebdesign,

    Sure we can help with this kind of thing. Now, I haven't tested this exactly, but my understanding is that you can do this in your query args.

    "meta-key" => "SF_Gallery_Order",
        "orderby" => "meta_value",
        "order" => "ASC"

    Checkout this page for more info, specifically the orderby "meta_value" part


    Thanks for being a WPMU DEV member. Let us know if we can be of any further assistance.
    Best regards, Alexander Rohmann

  • wiredstudios
    • The Incredible Code Injector


    I am a little confused around arrays.

    So this was the complete query

    query_posts(array('posts_per_page'=>'12', 'meta_key' => _thumbnail_id, 'meta_key' => 'SF_Gallery_Order', 'orderby' => 'meta_value'));

    Slightly Confused over how orderby 'knows' which 'meta_key' to sort by and why first meta_key doesn't require a meta_value.

    But it all seams to work so thankyou!

  • Alexander
    • DEV MAN’s Mascot


    Great to hear it's working! I can actually help clear things up for you a bit here. So to briefly cover arrays first, a normal PHP array looks like this:

    $args = array('Item1', 'Item2', 'Item3');

    It's simply a collection of data. If you want "Item1" you can retrieve it it with $args[0]

    An associate array looks like this:

    $args = array('key' => 'value', 'name' => 'Alexander');

    You access the values, by referencing the keys. So having $args['name'] will allow you to retrieve "Alexander"

    So hopefully that gives a little more insight into Arrays. Wordpress uses them alot because it's an easy way to pass a whole bunch of information into a function with only one parameter.

    Here's why your query is working: The "meta_key" tells WP which custom value to retrive in your post. The simplest way to explain it is that it takes the data from "meta_key" and stores it during this loop as "meta_value"

    Then when it comes to ordering, it knows to use the meta value that it just looked up.

    Hope this clears things up a bit, and glad you got it working!

    Best regards, Alexander Rohmann

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.