0

I have a functioning wp_query loop that is pulling all of the employees for a company from a custom post type. I figured out how to display the employees in alphabetical order by last name. I now need to show the owner of the company at the very start of the list of employees on the front of the site but still need the rest of the employees to flow in rows.

I know that I could use 2 separate loops but that seems like overkill when the first loop would only be used to pull a certain post and display it.

Is there a way to build a loop that pulls a single post and always displays it first and then runs the loop to display the rest of the results in alphabetical order by last name?

Here are the loop and part of the display code as I have it right now:

<div class="row team-row">
                    <?php add_filter( 'posts_orderby', 'posts_orderby_lastname' );
                        $loop = new WP_Query( array(
                        'post_type' => 'team',
                        'location'  => $location->slug
                        ) );
                    ?>

                    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

                        <div class="col-md-2 team-member">
                            <?php the_post_thumbnail('medium'); ?>
                            <h3 class="entry-title"><?php the_title( '' ); ?></h3>
                            <span class="team-position"><?php the_field('position'); ?></span><br />
                            <span class="team-phone"><?php the_field('phone'); ?></span><br />

                            <button class="btn btn-small team-btn" data-toggle="modal" data-target="#team-<?php the_ID(); ?>">
                                View Bio
                            </button>

                            <div class="modal fade" id="team-<?php the_ID(); ?>" tabindex="-1" role="dialog" aria-labelledby="team-<?php the_ID(); ?>Label" aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                                            <h4 class="modal-title" id="team-<?php the_ID(); ?>Label"><?php the_title( '' ); ?></h4>
                                        </div>
                                        <div class="modal-body">
                                            <div class="entry-content">
                                                <?php the_content(); ?>
                                            </div>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endwhile; ?>
1

1 Answer 1

0

What I can think of a way to do this is this, you get_post the post you would like to show on the first, it will return the WP Post Object, then query the custom post type using get_posts, excluding the ID of that post and finally push it to the array that will be given to you by get_posts.

//say this is the first post you want to show
$first_post = get_post( 23 );

//query all posts excluding the first post you want to display
$all_post_except_first = get_posts( array(
        'post_type' => 'your_custom_post_type_here',
        'numberposts' => -1,
        'post__exclude' => 23,
) );

//then put it on the first of the array
array_push( $all_post_except_first, $first_post );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.