I’m trying to change the sort order of a custom field in a custom post type.

I want it to be be alphabetical by the ‘location’ name.

It’s sorting by location, but not keeping them in alphabetical order.

Here is my code:

<?php foreach ( $tags as $tag) : $term = get_term_by("slug", $tag, "post_tag", "ARRAY_A");?>
                            <?php   $i =1; 
                                    $args = array(
                                        'tag' => $tag,
                                        'post_type' => 'jobs',
                                        'orderby' => 'meta_value',
                                        'order' => 'ASC',
                                        'meta_key' => 'location',
                                        'nopaging' => 'true'
                                    $query = new WP_Query( $args ); $length = count($query->posts); 
                                    if ($query -> have_posts()) : ?>
                                    <h4 class="section-title"><?php echo $term["name"] ;?></h4>
                                                        <h4> Position</h4>
                                                        <h4> Deadline</h4>
                                                        <h4> Pay & Hours</h4>
                                                        <h4> Location</h4>
                                                        <h4> Apply Online</h4>
                                        <?php while ( $query->have_posts()): ?>
                                            <?php $query -> the_post();?>
                                            <tr align="left" valign="top">
                                                <td style="width:140px"><a href="<?php the_field("job_description") ;?>" target="_blank"><?php the_title() ;?></a></td>
                                                <td style="width:140px"><?php the_field("deadline"); ?></td>
                                                <td style="width:140px"><?php the_field("pay_hours"); ?></td>
                                                <td style="width:140px"><?php the_field("location"); ?></td>
                                                <td style="width:140px"><a href="<?php echo home_url( '/' ); ?>job-application?field1=<?php the_title() ;?>&field1258=<?php the_field("location")?>&field1280=<?php echo urlencode(get_field("ymca_email_contact"));?>" target="_new">Apply Now</a></td>
                            <?php   endwhile; ?>

What is my issue?


Read more here: Sort alphabetically by custom field


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: