admin管理员组文章数量:1410712
How do I order following custom posts by category like so:
Cat title
player 1
player 2
Cat title 2
Player 5
player 7
As far to the code below is where my knowledge goes so any advise is very much appreciated!
<?php
$player_tax_terms = wp_get_object_terms( $post->ID, 'players_cat', array('fields' => 'ids') );
$args = array(
'post_type' => 'players',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'players_cat',
'field' => 'id',
'terms' => $player_tax_terms
)
),
'post__not_in' => array ($post->ID),
'meta_key' => 'player_number',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$related_items = new WP_Query( $args );
if ($related_items->have_posts()) : while ( $related_items->have_posts() ) : $related_items->the_post(); ?>
<a href="<?php the_permalink() ?>" class="cell player">
<?php if ( has_post_thumbnail() ): ?>
<?php the_post_thumbnail('thumbnail'); ?>
<?php else: ?>
<figure class="avatar"></figure>
<?php endif; ?>
<div class="title">
<?php the_title( '<h5>', '</h5>' ) ?>
<?php if( get_field('player_position') ): ?>
<p><?php the_field('player_position'); ?></p>
<?php endif; ?>
</div>
<?php if( get_field('player_number') ): ?>
<span><?php the_field('player_number'); ?></span>
<?php endif; ?>
</a>
<?php endwhile; endif;
wp_reset_postdata();
?>
</div>
</div>
How do I order following custom posts by category like so:
Cat title
player 1
player 2
Cat title 2
Player 5
player 7
As far to the code below is where my knowledge goes so any advise is very much appreciated!
<?php
$player_tax_terms = wp_get_object_terms( $post->ID, 'players_cat', array('fields' => 'ids') );
$args = array(
'post_type' => 'players',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'players_cat',
'field' => 'id',
'terms' => $player_tax_terms
)
),
'post__not_in' => array ($post->ID),
'meta_key' => 'player_number',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$related_items = new WP_Query( $args );
if ($related_items->have_posts()) : while ( $related_items->have_posts() ) : $related_items->the_post(); ?>
<a href="<?php the_permalink() ?>" class="cell player">
<?php if ( has_post_thumbnail() ): ?>
<?php the_post_thumbnail('thumbnail'); ?>
<?php else: ?>
<figure class="avatar"></figure>
<?php endif; ?>
<div class="title">
<?php the_title( '<h5>', '</h5>' ) ?>
<?php if( get_field('player_position') ): ?>
<p><?php the_field('player_position'); ?></p>
<?php endif; ?>
</div>
<?php if( get_field('player_number') ): ?>
<span><?php the_field('player_number'); ?></span>
<?php endif; ?>
</a>
<?php endwhile; endif;
wp_reset_postdata();
?>
</div>
</div>
Share
Improve this question
edited Dec 13, 2019 at 22:07
Bram Roos
asked Dec 13, 2019 at 19:55
Bram RoosBram Roos
11 bronze badge
1
- Anyone knows how to do it? Any help is much appreciated. – Bram Roos Commented Dec 17, 2019 at 13:45
1 Answer
Reset to default 0You have a couple hurdles to handle ...
1) You can have multiple Category codes for each post, so they are not normal query variables (not really a hurdle - but you may get repeat occurrances of the same post under multiple categories).
2) Category titles are 1 linkage 'further' away (in terms of SQL queries) than the category code. i.e. the post is connected to the Category code(s), then those codes are connected to the code titles. This is more of a challenge.
THIS POST shows a similar issue with a solution of getting all the categories, then running a filtered query for each category, and displaying the post list for that category. Perhaps that is close enough to what you are looking for?
本文标签: wp queryRelated categories order posts by category
版权声明:本文标题:wp query - Related categories order posts by category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744892007a2630839.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论