订购WP_Query结果

时间:2015-02-28 12:31:05

标签: php wordpress

如果我手动分配元值'meta_value' => '4-5',则以下查询有效,但我需要它来返回每个$cartArr值的帖子。怎么能做到这样呢?

  <ul id="order-box" class="list-group">';

print_r($cartArr); //Array ( [0] => 3-6 [1] => 4-5 )


        function  query_group_by_filter($groupby){
             global $wpdb;
             return $wpdb->postmeta . '.meta_key ';
          }

          add_filter('posts_groupby', 'query_group_by_filter');

          $the_query = new WP_Query(array(
            'post_type' => 'clasa',
            'post_status' => 'publish',
            'meta_key' => 'twin_id',
            'meta_value' => '4-5'
          )

          );

          remove_filter('posts_groupby', 'query_group_by_filter');

          if ( $the_query->have_posts() ) {

                  while ( $the_query->have_posts() ) {
                      $the_query->the_post();
                      echo '<li class="list-group-item" id="remli-'.$post->ID.'">' . get_the_title() . '<a class="right remove" id="remove-'.get_post_meta($post->ID, 'twin_id', true).'" href="#">x</a>';
                  }

              } else {
                  // no posts found
              }
              /* Restore original Post Data */
              //wp_reset_postdata();


echo '</ul>';

1 个答案:

答案 0 :(得分:0)

需要添加foreach循环。比如下面提到的foreach($cartArr as $cartArrr)。如果工作请投票给我。

<ul id="order-box" class="list-group">';
<?php
print_r($cartArr); //Array ( [0] => 3-6 [1] => 4-5 )


        function  query_group_by_filter($groupby){
             global $wpdb;
             return $wpdb->postmeta . '.meta_key ';
          }

          add_filter('posts_groupby', 'query_group_by_filter');

        foreach($cartArr as $cartArrr) {

          $the_query = new WP_Query(array(
            'post_type' => 'clasa',
            'post_status' => 'publish',
            'meta_key' => 'twin_id',
            'meta_value' => $cartArrr
          )

          );

          remove_filter('posts_groupby', 'query_group_by_filter');

          if ( $the_query->have_posts() ) {

                  while ( $the_query->have_posts() ) {
                      $the_query->the_post();
                      echo '<li class="list-group-item" id="remli-'.$post->ID.'">' . get_the_title() . '<a class="right remove" id="remove-'.get_post_meta($post->ID, 'twin_id', true).'" href="#">x</a>';
                  }

              } else {
                  // no posts found
              }
              /* Restore original Post Data */
              //wp_reset_postdata();
        }

echo '</ul>';
相关问题