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

本文标签: wp queryRelated categories order posts by category