1

I did group my search results by post type (artists, events, posts) and did put each of them in a container. The next step is give each of them it's own order within the container:

  • Artists - From A-Z
  • Events - All upcoming events first
  • Posts - Date desc

I kinda know how to set up the queries for each, but i don't know how to make it functional inside the code i already have... Who can help me out?

This is a snippet from my searchresults.php:

                    <?php   
                    $last_type="";
                    $typecount = 0;
                    while (have_posts()) :
                    the_post();
                    if ($last_type != $post->post_type){
                    $typecount = $typecount + 1;
                    if ($typecount > 1){
                        echo '</div><!-- close container -->'; //close type container
                    }
                    // save the post type.
                    $last_type = $post->post_type;
                    //open type container
                    switch ($post->post_type) {
                        case 'cpt_artist':
                        echo "<div class=\"artistsearch-container\"><h2>Artiesten</h2>";
                        break;
                case 'cpt_event':
                        echo "<div class=\"eventsearch-container\"><h2>Evenementen</h2>";
                break;
                case 'post':
                        echo "<div class=\"postsearch-container\"><h2>Blog Results</h2>";
                        break;

                    }
                    } 
            ?>

                <?php if('post' == get_post_type()) : ?>
            <h2 class="blog">
         <a href="<?php the_permalink(); ?>">
           <?php the_title(); ?>
         </a>
      </h2>
    <?php endif; ?>

        <?php if('cpt_event' == get_post_type()) : 

                        $event_date      = get_post_meta( get_the_ID(), 'ci_cpt_event_date', true );
                        $event_time      = get_post_meta( get_the_ID(), 'ci_cpt_event_time', true );
                        $event_timestamp = strtotime( $event_date . ' ' . $event_time, current_time( 'timestamp' ) );
                        $event_enddate      = get_post_meta( get_the_ID(), 'ci_cpt_event_enddate', true );
                        $event_endtime      = get_post_meta( get_the_ID(), 'ci_cpt_event_endtime', true );
                        $event_endtimestamp = strtotime( $event_enddate . ' ' . $event_endtime, current_time( 'timestamp' ) );
                        $event_location  = get_post_meta( get_the_ID(), 'ci_cpt_event_location', true );
                        $event_venue     = get_post_meta( get_the_ID(), 'ci_cpt_event_venue', true );
                        $event_timestamp = strtotime( $event_date . ' ' . $event_time, current_time( 'timestamp' ) );

                        ?>
                        <div class="search-results-item-event"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br/>
                        <?php if (empty($event_enddate)){?>
                            <span><?php echo date_i18n( get_option('date_format'), $event_timestamp ); ?> &middot; <?php echo $event_venue; ?>, <?php echo $event_location; ?></span>
                        <?php
                        ;}
                        else {?>
                            <span><?php echo date_i18n( get_option('date_format'), $event_timestamp ); ?> - <?php echo date_i18n( get_option('date_format'), $event_endtimestamp ); ?> &middot; <?php echo $event_venue; ?>, <?php echo $event_location; ?></span>
                        <?php
                        ;}
                        ?>
                        </div>
        <?php endif; ?>

    <?php if('cpt_artist' == get_post_type()) : ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php endif; ?>


<?php endwhile; ?>

<?php else : ?>
<div class="open-a-div">
<p>No results found.</p>    

<?php endif; ?>       
</div>

            <?php ci_pagination(); ?>
        </div>

This is a snippet from my functions.php:

add_filter('posts_orderby', 'group_by_post_type', 10, 2);
function group_by_post_type($orderby, $query) {
    global $wpdb;
    if ($query->is_search) {
        return $wpdb->posts . '.post_type ASC';
    }
    // provide a default fallback return if the above condition is not true
    return $orderby;
}

1 Answer 1

1

Here's one suggested high level approach to get the desired effect.

  • Rather than perform all your operations in one big while loop in your searchresults.php, try:

    1. establishing three arrays corresponding to each type- you can do this in one while loop.
    2. then sort each array as needed (refer to PHP's Sorting Arrays documentation for the various ways to sort a multidimensional array)
    3. then output the section heading and loop through your teasers of posts for each type.
  • Alternatively, in your group_by_post_type function's if statement, you can perform steps 1 & 2 above, and then merge the arrays, returning the merged result and keeping your searchresults.php pretty much intact as you've posted it.

0

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.