dongxuan1660 2017-04-07 13:56 采纳率: 100%
浏览 44

如何将wp_query的输出分成单独的部分

I have a WP Query that is checking a custom post types metafields.

I have two custom post types:

  1. Country
  2. Media

The country custom posts type is simply created to pull the content of the media post type into.

The Media CPT contains 2 metafields:

  1. The first metafield is a media filter selection that allows the selection of "TV", "Radio", "Digital" and has this ID _rtl_media_filter.
  2. The second contains a country to associate the post type to and has this ID _rtl_country_filter, which is pulling the country CPT name into a dropdown and can be selected from within the media post type.

The aim is to display all the media types that are assiciated with the country CPT when the country CPT is viewed.

I have this loop, which works. But am wondering if I can group a single loops item by media type _rtl_media_filter? Rather than creating multiple loops for each?

<?php
    // Grab this posts ID
    $post = $wp_query->post;
    $this_id = $post->ID;

    // Query the meta vaules against the posts ID
          $args = array(
            'post_type'     => 'media',
            'meta_query'    =>  array(
                    'relation' => 'OR',
                        array( // TV
                                'relation' => 'AND',
                                array(
                                        'key' => '_rtl_media_filter',
                                        'value' => 'tv',
                                        'compare' => '=',
                                ),
                                array(
                                        'key' => '_rtl_country_filter',
                                        'value' => $this_id,
                                        'compare' => '=',
                                ),
                        ),
                        array( // DIGITAL
                                'relation' => 'AND',
                                array(
                                        'key' => '_rtl_media_filter',
                                        'value' => 'digital',
                                        'compare' => '=',
                                ),
                                array(
                                        'key' => '_rtl_country_filter',
                                        'value' => $this_id,
                                        'compare' => '=',
                                ),
                        ),
                        array( // RADIO
                                'relation' => 'AND',
                                array(
                                        'key' => '_rtl_media_filter',
                                        'value' => 'radio',
                                        'compare' => '=',
                                ),
                                array(
                                        'key' => '_rtl_country_filter',
                                        'value' => $this_id,
                                        'compare' => '=',
                                ),
                        ),
                ),
           'posts_per_page' => -1,
        );

        $the_query = new WP_Query( $args );

        if($the_query -> have_posts()) :
            while ($the_query -> have_posts()) : $the_query -> the_post();

            echo '<h2>' . the_title() . '</h2>';

            endwhile;
            wp_reset_postdata();
        endif;

?>

This is how I would like the output:

|TV|

ITV | ITV2 | ITV3

|RADIO|

Radio 1 | Radio2

|DIGITAL|

HUB 1 | HUB 2

  • 写回答

1条回答 默认 最新

  • douxi8759 2017-04-07 14:51
    关注

    So After Antonis pointed out about ordering by meta_value I figured out the way it can be done. For anyone else trying this the solution lies with these couple of lines:

           'orderby' => 'meta_value',
           'meta_key' => '_rtl_media_filter',
    

    Add those to the bottom of your loop query.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)