1

I have a custom query:

<?php
query_posts(array(
'post_type' => 'properties',
'meta_key' => 'pd_city',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 5,
'paged' => get_query_var('paged')
));
?>

But it doesn't seem to be working. The posts on this page should be listed alphabetically by city (Burnaby, New Westminster, Richmond, Surrey), but they are listed in a random order (I'm not sure how they are being ordered).

Not sure what I'm missing. I created the 'properties' post_type via the 'Post Meta' Wordpress plugin if that makes a difference.

http://www.professionalrentals.ca/new-properties-test-page/


Attempt using WP_Query - Also DOES NOT WORK

<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
    'post_type' => 'properties',
    'meta_key' => 'pd_city',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'posts_per_page' => 5,
    'paged' => $paged
);
$the_query = new WP_Query( $args );
?>

<?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
2
  • 1
    I think I have told you this previously, but if not, here I go: NEVER EVER use query_posts. It messes up the main query, is slow, breaks page functionality, breaks pagination, and reruns queries. It is just plain evil. Use pre_get_posts to alter the main query, or use WP_Query or get_posts to construct custom queries for secondary queries Oct 21, 2015 at 19:23
  • I tried both of those things already, and it gives me the same result. I actually went back to my other post and looked at all the suggestions and tried them before posting.
    – KVDD
    Oct 21, 2015 at 20:31

3 Answers 3

2

Piter is right, don't use query_posts.

You could do something like:

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
    'post_type' => 'properties',
    'meta_key' => 'pd_city',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'posts_per_page' => 5,
    'paged' => $paged
);

$query = new WP_Query( $args );

Just make sure you have the values right for your post_type and meta_key params. I.e, I use an underscore for all my meta keys: _properties.

I have a nearly identical query running on a client's project here: http://centerpoint.com/properties/?pgd=1&v=list&filter=1&list_type=all&sortby=city

6
  • I've edited my post above to include the code using WP_Query, but it is giving me the same output as query_posts. I'm not sure if you're familiar with the "Post Meta" plugin, but I have a section with a meta_key = properties_1, and in this section I have my city with a meta_key = pd_city. Now could this nested meta_key thing be the reason I am having issues?
    – KVDD
    Oct 21, 2015 at 21:02
  • Do you have access to the database? If you do, it might be better to pull up a property post type in the wp_postmeta table and see what meta_keys are stored with it.
    – darrinb
    Oct 21, 2015 at 21:18
  • I do have access. I can find pd_city 4 times in the 'meta_key' column (for the 4 test properties I have added). Not sure what that means though...
    – KVDD
    Oct 21, 2015 at 21:29
  • If each of those properties has pd_city as a meta key with a city name as a value, the query should work. Using your code above, debug your query by print_r($the_query) to see what query WP is building. That may help determine where the error is.
    – darrinb
    Oct 21, 2015 at 22:14
  • Is there something in specific I'm looking for? This spits out a whole wack of stuff...
    – KVDD
    Oct 21, 2015 at 23:20
0

I took a quick look at the Post Meta plugin you are using, and it appears to save it's meta values as serialized arrays. Therefore your query is never going to work - WP doesn't know how to order a bunch of serialized arrays.

If you really want to use a plug-in (rather than build your own meta fields), I'd recommend you dump Post Meta and switch to Advanced Custom Fields. Note you'll need to deactivate Post Meta for ACF to work.

4
  • Is there a way I can do a nested query or something? Like query one meta key, to get info, and then query the next meta key inside that? I'm too many hours into this project to scrap it and start over.
    – KVDD
    Oct 22, 2015 at 16:28
  • There's no way to do this with nested queries. You could just select everything that uses the pd_city meta key, then order them in PHP. But this would be complicated. I still recommend switching plugins. It would just mean re-adding the cities.
    – vancoder
    Oct 22, 2015 at 18:22
  • Incidentally, after I pointed out this flaw in Post Meta, the plugin author responded "Our next version is coming soon and this version will have the wordpress default meta function."
    – vancoder
    Oct 22, 2015 at 18:23
  • He said he was releasing a new version 'soon' about 6 months ago as well. Hmmmm... I wonder if I can convince my client to have it sorted by date for now until this new version is out. Cause I'm banging my head against the wall looking for solutions.
    – KVDD
    Oct 22, 2015 at 19:12
0

Okay I have a really ridiculous work around that ends up running the query multiple times (I'm sure I'll get torn apart in the comments for this), but it works for now so here goes.

This only works if you know exactly which fields you will be sorting between. AKA I have a list of cities I need ordered alphabetically. And I know there are only these cities. (I guess you could run a separate query to get this array from the DB but I didn't want MORE queries)

<?php
$listOfCities = array('burnaby', 'delta', 'ladner', 'new westminster', 'north vancouver', 'richmond', 'tsawwassen', 'vancouver', 'white rock');

foreach ($listOfCities as $cities) {

$args = array(
'post_type' => 'properties',
'meta_key' => 'pd_city',
'meta_query' => array(
        array(
            'key' => 'pd_city',
            'value' => $cities,
            'compare' => 'LIKE'
        )),
'order' => 'ASC'
);
$the_query = new WP_Query( $args );

if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();

    echo get_field(pd_city);

endwhile; endif; wp_reset_postdata();

}
?>

And after all that the client wanted it ordered a different way, but since it processes it based on what's first in the array to last, I just changed the array to:

$listOfCities = array('vancouver', 'richmond', 'burnaby', 'new westminster', 'north vancouver', 'white rock', 'delta', 'ladner', 'tsawwassen');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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