简体   繁体   English

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

[英]How can I separate the output of a wp_query into separate sections

I have a WP Query that is checking a custom post types metafields. 我有一个WP查询正在检查自定义的帖子类型元字段。

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: 媒体CPT包含2个元区域:

  1. The first metafield is a media filter selection that allows the selection of "TV", "Radio", "Digital" and has this ID _rtl_media_filter . 第一个元字段是媒体过滤器选择,允许选择“TV”,“Radio”,“Digital”并具有此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. 第二个包含将帖子类型关联到的国家/地区,并且具有此ID _rtl_country_filter ,它将国家/地区CPT名称拉入下拉列表,并且可以从媒体帖子类型中选择。

The aim is to display all the media types that are assiciated with the country CPT when the country CPT is viewed. 目的是在查看国家/地区CPT时显示与国家/地区CPT相关的所有媒体类型。

I have this loop, which works. 我有这个循环,这是有效的。 But am wondering if I can group a single loops item by media type _rtl_media_filter ? 但我想知道我是否可以按媒体类型_rtl_media_filter对单个循环项目进行_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 | ITV | ITV2 | ITV2 | ITV3 ITV3

|RADIO| | RADIO |

Radio 1 | 收音机1 | Radio2 第二广播电台

|DIGITAL| |数码|

HUB 1 | HUB 1 | HUB 2 HUB 2

So After Antonis pointed out about ordering by meta_value I figured out the way it can be done. 因此,在Antonis指出按meta_value排序后,我想出了它可以做到的方式。 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. 将它们添加到循环查询的底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM