0

我目前正在遍历所有帖子并显示 post_meta 值,如下所示:

global $wpdb;
$table =  $wpdb->prefix . 'postmeta';
$theid = get_the_id();
$getLowestPrice = $wpdb->get_results("SELECT * FROM $table WHERE meta_value = '$theid'");
foreach ( $getLowestPrice as $post ){

get_post_meta( $post->post_id, '_wholesale_price', false );

}

有没有办法对结果进行排序,最低 -> 最高?目前它们正在随机显示,或者在输入时显示。

4

2 回答 2

0

这对我来说有点不对劲。如果您尝试按元键的值对帖子进行排序(除非我弄错了,这就是您正在做的事情),那么我通常会使用WP_Query()

global $wp_query;

$args = array(
  'post_type' => '[YOUR POST TYPE]', 
  'meta_key' => '_wholesale_price', 
  'orderby' => 'meta_value meta_value_num', // meta_value_num for value
  'order' => 'ASC' // or DSC for low/high
);

$wp_query - new WP_Query( $args );

if ( $wp_query->have_posts() ) {
  while ( $wp_query->have_posts() ) {
    $wp_query->the_post(); 

    // Your loop

  }
}
于 2013-08-05T19:53:35.897 回答
0

使用以下代码

<?php 
 global $wpdb;
 $table =  $wpdb->prefix . 'postmeta';
 $theid = get_the_id();
 $getLowestPrice = $wpdb->get_results("SELECT * FROM $table WHERE meta_value = '$theid'");
$all_post = array();
foreach ( $getLowestPrice as $post ){
   $all_post[] = $post->post_id; 

}
$query = new WP_Query( array( 'post__in' => $all_post, 'orderby' => 'meta_value', 'meta_key' => '_wholesale_price','order' => 'ASC') );

// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
    $query->the_post();
    echo '<div>' . get_post_meta( $get_the_ID(), '_wholesale_price', false );() . '</div>';
}
 } else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>`
于 2013-08-05T15:01:20.200 回答