WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom post type and I want to be able to control the order that the posts are displayed by defining the order in an array, and then checking the posts meta_value against the array to see where it should sit in the order of posts.

Using the code below I can get the dealers who's 'Postcode' is one of the values defined in the $target_postcodes array of the 10 closest postcodes to the current users location.

$posts = get_posts(array(
        'post_type' => 'dealer',
        'meta_key' => 'Postcode',
        'meta_value' => $target_postcodes,
        'post_status' => 'publish'

Is it possible for me to sort the posts by the order of the values in the 'meta_values' query?

For example, if we use the following post values:

[Name]    [Postcode]
Dealer 1  1234
Dealer 2  2345
Dealer 3  3456
Dealer 4  4567

And then the define the $target_postcodes array as

$target_postcodes = 4567,1234,3456,2345

I would then want the loop to display the dealers in the following order

Dealer 4
Dealer 1
Dealer 3
Dealer 2

Thanks, Brad

share|improve this question

You should be able to use a custom query to achieve this. This codex example might be of use.

share|improve this answer
The problem is that we don't want to sort the dealers asc/desc by the $target_postcodes values, we want the system to sort the posts as per the position in the $target_postcodes array. – Brad Farleigh Jun 25 '12 at 4:55
I have updated the question with an example of how I want the data sorted – Brad Farleigh Jun 25 '12 at 5:06
The simplest solution I can think of is to accept another meta value along with the postal code that captures the distance and then use that to sort the posts. If you want to do it using the target_postcodes array - I think you need to do it programmatically. In that case, you may not be able to use the pagination provide by WP and the solution becomes more complicated. – KDM Jun 25 '12 at 5:38
Thanks for your help. The order of the $target_postcodes array changes depending on the postcode of the user who's viewing the page (so they are ordered closests > farthest) and the distance can't be stored in the database as it will be different depending on the users postcode, and I don't want to store the distance values for every single postcode combination :( – Brad Farleigh Jun 25 '12 at 5:55
May be you can save the longitude&latitude for each postal code and then calculate the distance from the user's postal code. You might be even use this in the order by clause of the query and still use get_posts. – KDM Jun 25 '12 at 6:22
up vote 0 down vote accepted

I ended up querying each postcode separately then compiling the results into the $posts[] array using array_merge. This meant that the final $posts[] array contained all my posts in distance order and I could loop through it normally.

I understand this is a really inneficient way of doing it and would appreciate if anyone could give some feedback/improvements!

The code was similar to below:

/** My array of postcodes, from closest to farthest**/

 Loop through each postcode and grab all the dealers 
where the postcode matches the one in that loop
foreach($postcodes as $the_postcode){
    $subSet = get_posts(array(
        'post_type' => 'dealer',
        'meta_key' => 'Postcode',
        'meta_value' => $the_postcode,
        'post_status' => 'publish'
    Append the new results to the the end of the $posts array
    $posts = array_merge($posts,$currentSet);

/** Display final list of post titles in the order of values defined in $postcodes**/
    foreach($posts as $post)
    echo the_title().'<br />';
share|improve this answer
Using meta_query with relation OR instead of meta_key might be useful. – KDM Jun 29 '12 at 11:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.