Php 可湿性粉剂位置升序

Php 可湿性粉剂位置升序,php,arrays,wordpress,sorting,Php,Arrays,Wordpress,Sorting,我试图在Word Press中对订单位置进行排序,这些位置存储在posts元数据中 在我的主页上有一个下拉框,用户可以选择我希望他们从a-Z订购的位置 <?php $args_location = array( 'posts_per_page' => -1 ); $lastposts

我试图在Word Press中对订单位置进行排序,这些位置存储在posts元数据中

在我的主页上有一个下拉框,用户可以选择我希望他们从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 }


实际上,您可以在一个步骤中获取它们,因为get_posts只使用WP_查询。这:

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

然后,您的帖子将按帖子位置进行排序。

非常感谢您的帮助,我正在慢慢学习PHP,我只需更改一件事,您的答案中添加了一个。而不是,在“meta_key”=>“post_location”之后。
 $args_location = array(
     'posts_per_page' => -1,
     'orderby' => 'meta_value',
     'meta_key' => 'post_location',
     'order' => 'ASC'
 );
 $lastposts = get_posts( $args_location );