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="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" 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="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" 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>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0