3

I have post titles which begin with a price, eg: $199,900 - 123 Street. I am using the code below, and I'm unable to get the order by title ascending to work properly. Any suggestions please.

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
 'cat' =>21,
 'posts_per_page' => 999999,
 'paged' => $paged,
 'orderby' => 'title',
 'order' => 'ASC',
 );
$the_query = new WP_Query($args);
?>
<div id="listings">

<?php if ($the_query->have_posts()) : ?>

<?php while ($the_query->have_posts()): $the_query->the_post(); ?>
        <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
            <div class="title1 active_listing">
                <?php
                if (in_category('6')) {
                the_title();
                } else {
                ?>

                <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
                <?php
                }
                ?>
            </div>

                <div class="alignleft thumb"><?php the_post_thumbnail('thumbnail'); ?>
<br />
</div>
        <div class="content"><?php the_excerpt(); ?> </div>

                <?php
                if (in_category('6')) {
                ?>
<br />
                <?php
                } else {
                ?>
 <a href="<?php the_permalink() ?>" rel="bookmark"> View Details</a>
 <br />
                <?php
                }
                ?>

        </div>

    <?php endwhile; wp_reset_postdata();?>

<?php else : ?>

    <h2 class="center">Not Found</h2>
    <p class="center">Sorry, but you are looking for something that isn't here.</p>
    <?php get_search_form(); ?>

<?php endif; ?>

</div>
4

If you're trying to get them to order by ascending price, the problem is that ordering by title is a string comparison, not numeric. A solution would be to put the price in a custom field and orderby your custom field rather than title. You would add a meta_key param with the name of your meta key, and orderby meta_value_num. See WP_Query in Codex for more info.

Also- if you want all posts returned (which I assume is what you're trying to do with setting it to such a high number), you can set posts_per_page to -1 instead and it will remove the LIMIT clause from the query.

| improve this answer | |
  • Thank you very much Milo! Works beautifully! I've seen the meta_key and meta_value usage while I was searching for an answer, but never understood it completely and its connection to custom fields. – spiderling Oct 7 '11 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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