WP 位置顺序升序

WP Locations Order Ascending

我正在尝试对 Word Press 中的顺序位置进行排序,这些位置存储在帖子元数据中。

在我的主页上有一个下拉框供用户选择 select 我希望他们可以从 A -Z 订购

    <?php

                                                $args_location = array( 'posts_per_page' => -1 );
                                                $lastposts = get_posts( $args_location );

                                                $all_post_location = array();
                                                foreach( $lastposts as $post ) {
                                                    $all_post_location[] = get_post_meta( $post->ID, 'post_location', true );
                                                }

                                                $directors = array_unique($all_post_location);
                                                asort($directors);
                                                foreach ($directors as $director) { ?>
                                                    <option value="<?php echo $director; ?>"><?php echo $director; ?></option>
                                                <?php }

任何帮助将不胜感激

我确实尝试这样做来排序

    <?php

                                                $args_location = array( 'posts_per_page' => -1 );
                                                $lastposts = get_posts( $args_location );

                                                $all_post_location = array();
                                                foreach( $lastposts as $post ) {
                                                    $all_post_location[] = get_post_meta( $post->ID, 'post_location', true );
                                                }

                                                $directors = array_unique($all_post_location);
                                                asort($directors);
                                                **sort($directors);**
                                                foreach ($directors as $director) { ?>
                                                    <option value="<?php echo $director; ?>"><?php echo $director; ?></option>
                                                <?php }

您实际上可以一步得到它们,因为 get_posts 只需使用 WP_Query。这个:

 $args_location = array(
     'posts_per_page' => -1,
     'orderby' => 'meta_value',
     'meta_key' => 'post_location',
     'order' => 'ASC'
 );
 $lastposts = get_posts( $args_location );

您的帖子将按 post_location 排序。