我正在尝试在Word Press中对订单位置进行排序,这些位置存储在帖子元数据中。
我的主页上的是一个位置下拉框,用户可以选择我希望从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 }
答案 0 :(得分:1)
你实际上可以一步到位,因为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排序。