wp query - Related categories order posts by category

How do I order following custom posts by category like so:Cat titleplayer 1player 2Cat title 2Player 5player 7As far to

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
Add a comment  | 

1 Answer 1

Reset to default 0

You 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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744892007a4599471.html

相关推荐

  • wp query - Related categories order posts by category

    How do I order following custom posts by category like so:Cat titleplayer 1player 2Cat title 2Player 5player 7As far to

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信