php - How can I seperate the output of a wp_query into seperate sections -
i have wp query checking custom post types metafields.
i have 2 custom post types:
- country
- media
the country custom posts type created pull content of media post type into.
the media cpt contains 2 metafields:
- the first metafield media filter selection allows selection of "tv", "radio", "digital" , has id
_rtl_media_filter
. - the second contains country associate post type , has id
_rtl_country_filter
, pulling country cpt name dropdown , can selected within media post type.
the aim display media types assiciated country cpt when country cpt viewed.
i have loop, works. wondering if can group single loops item media type _rtl_media_filter
? rather creating multiple loops each?
<?php // grab posts id $post = $wp_query->post; $this_id = $post->id; // query meta vaules against 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 how output:
|tv|
itv | itv2 | itv3
|radio|
radio 1 | radio2
|digital|
hub 1 | hub 2
so after antonis pointed out ordering meta_value figured out way can done. else trying solution lies these couple of lines:
'orderby' => 'meta_value', 'meta_key' => '_rtl_media_filter',
add bottom of loop query.
Comments
Post a Comment